views:

208

answers:

2

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.

+1  A: 

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.

openCage
Instanceof checks will still work, because proxies are still instances of sub classes of your class. What wouldn't work is an equals or identity comparision of the class object (as provided by getClass()).
Willi
+3  A: 

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.

rhu
Well the object is eagerly loaded and since I am getting a proxy object in this particular call, I guess you cannot guarentee that the object will be either. Interestingly tho, when I call DAO.getUser(String username) from method A, I get the User object but from method B calling the method in the exact same manor, I get the proxy.
predhme