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 is getting a return type of ObjectClass_$$_javassist_somenumber as the type. Which is throwing things off. I call the service method exactly the same as everywhere else, so why would hibernate return the proxy as opposed to the natural object?
I know there are ways to expose the “proxied” object, but I don’t feel like I should have to do that.
The query is simply
hibernateTemplate.find("from User u where u.username = ?", username)
I am using hibernate 3.3 btw.
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(), useHibernate.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.