-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problem with joined-subclass proxy
PostPosted: Thu Jul 20, 2006 4:16 pm 
Newbie

Joined: Sat May 22, 2004 4:49 pm
Posts: 4
I have a quick question regarding joined-subclasses.

Assuming I have a fairly simple hierarchy:

Code:
class A { }

class B extends A{}
class C extends A{}


My code does a store of a derived class (say class "B"). Then, it attempts to do a load specifying the super class ("A"), I am returned a proxy that is of the super class type (assuming the "B" object is no longer in a session cache).

For example:

Code:
B b = new B();
session.save( b );

session.clear();

Obj copy = session.load( A.class, b.getId() );


The type of the "copy" object is something like:

Code:
com.foo.A$$EnhancerByCGLIB$$1623f325


The proxy is derived directly from A. But, if I look in the CGLIB object at the actual implementation, it is of type "B".

Code:
((HibernateProxy)obj).getHibernateLazyInitializer().getImplementation()


To me, this seems like incorrect behavior, but maybe my assumptions are incorrect. I was under the impression that the proxy should be of the type found in the database. It correctly knows that it is a "B" object since the implementation object found in the proxy is of that type. In fact, the implemenation object now holds information that I can't access via the proxy since it is directly derived off of "A".

The problem appears to be that the proxy is created based on the type that is passed to the load method on session, rather than waiting to create the proxy based on the subclass type that is found when the read on the database is performed.

So, my question is. Is this expected behavior? If yes, why does Hibernate go through all of the trouble to load all of the data for the "B" class instance, and then embed it into a CGLIB proxy that doesn't allow access to that information.

Any help would be greatly appreciated.

--Chris.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 5:27 pm 
Newbie

Joined: Sat May 22, 2004 4:49 pm
Posts: 4
Well, after digging a bit, I found that if I use "get()" versus "load()" it works the way I would like it to. The weird thing is that the documentation doesn't really spell out any major differences between the two methods other than one returning null if the object isn't found. It appears that load() will proxy the object without doing a hit on the database, while get() actually hits the database so it can determine the proper type to return (relationships are proxied).

However, that didn't fix all my problems.

If I use the Criteria API, I still am getting partials back in the same manner as I mentioned above (superclass proxy, like when I was using "load"). However, If I use a straight HQL query, objects are returned similar to using "get()" on the session. Is there any way to have the Criteria API do something in a similar manner?

We are developing a small Query API on top of Criteria, so it would be great if I could get that to work.

As always, any help would be greatly appreciated.

Thanks.

--Chris.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.