Sunday, February 21, 2010

Hibernate proxy

Hibernate proxy

Proxies are created dynamically by subclassing your object at runtime. The subclass has all the methods of the parent, and when any of the methods are accessed, the proxy loads up the real object from the DB and calls the method for you. Very nice in simple cases with no object hierarchy. Typecasting and instanceof work perfectly on the proxy in this case since it is a direct subclass. By default Hibernate creates a proxy for each of the class you map in mapping file. This class contain the code to invoke JDBC. This class is created by hibernate using CGLIB.


Proxies are the mechanism that allows Hibernate to break up the interconnected cloud of objects in the database into smaller chunks, that can easily fit in memory. Proxies are created dynamically by subclassing your object at runtime. The subclass has all the methods of the parent, and when any of the methods are accessed, the proxy loads up the real object from the DB and calls the method for you.


A class can be mapped to a proxy instead to a table. When you actually call load on session it returns you proxy. This proxy may contain actual method to load the data. Object proxies can be defined in one of two ways. First, you can add a proxy attribute to the class element. You can either specify a different class or use the persistent class as the proxy.


For example:

<class name="Loc"
proxy="com.ch01.Loc">

lt;/class>

The second method is to use the lazy attribute. Setting lazy="true"is a shorthand way of defining the persistent class as the proxy.

3 comments:

athi,  September 8, 2010 at 10:42 PM  

You may please refer to the following link, and do let me know if it really helped http://freejavaclass.com/articles/articleshome.jsp?tutorialpath=designpattern

ReddYPrasaD Pidugu,  October 11, 2010 at 12:19 AM  

Hi..Useful concepts..Keep posting.. :) U can check http://JavaJ2eePlanet.blogspot.com for some more java interview questions.

Anonymous,  October 23, 2010 at 1:50 AM  

Thank you sooo much. ur concepts are really good. my no. 9032663244

Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP