Tuesday, April 4, 2017

What is Hibernate proxy?

By default Hibernate creates a proxy for each of the class you map in mapping file. This class is created by hibernate using CGLIB. 

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.

No comments: