I have a service method that calls a DAO which then returns an object from the database. This method is called from numerous parts of the system. However, one particular method
It is a proxied object in order to support lazy loading; basically as soon as you reference a child or lookup object via the accessor/getter methods, if the linked entity is not in the session cache, then the proxy code will go off to the database and load the linked object. It uses javassist to effectively dynamically generate sub-classed implementations of your objects (although I think it can be configured to use CGLIB too).
If it weren't proxied in this way it would be nigh-on impossible to implement seamless lazy loading.
I can't remember off the top-of-my-head whether if you use eager loading then whether the natural object would be returned instead. I wouldn't generally recommend using eager loading though especially if you have lots of linked child entities, as it can soon be a huge performance bottleneck as it will suck in every linked object into memory.
Also, if you need to discriminate on the class type, rather than using obj.getClass()
, use Hibernate.getClass(obj)
which will give you back the natural object class regardless of whether it is proxed or not: see the Hibernate API Javadocs here.
In my opinion this expression:
hibernateTemplate.find("from User u where u.username = ?", username)
Should always return POJO, not a proxy. This is because standard HQL/criteria returns non-proxied object, but objects of your original entity classes. This is different from lazy associations fetching:
@Entity
class X {
@ManyToOne(fetch = FetchType.LAZY)
private User user;
}
Getting X
object from the db here, we will have a lazy proxy in X.user
field (a proxied User
instance).
Now, it happens that doing from User where [...]
you sometimes have POJO and sometimes proxy object. Usually this is because in some executions User
object was first fetched from the db through associations (from X where [...]
query was called first in given hibernate session). Having already (proxied) User
instance, hibernate will reuse this instance even for your plain queries like from User where [...]
.
Hibernate returns proxies if not all members are resolved, i.e. the object is not complete. This is often a desired feature to improve performance and is (I think) the default setting in hibernate.
If you do not want proxies you can suppress lazy loading in the hbm.xml file, i.e. use eager loading. Please check the hibernate docs for the exact syntax.
To use the proxy object just never access a member directly but only via getter, even within member functions. Hibernate magic fills in the member when you get it. This way you never have to expose the object. Also don't use instanceof on potential proxy objects. But this is a code smell anyway.