-->
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.  [ 4 posts ] 
Author Message
 Post subject: Unnecessary Proxy?
PostPosted: Fri Jan 15, 2010 7:54 am 
Newbie

Joined: Thu Feb 28, 2008 4:39 am
Posts: 4
I have an object containing another object as a field. This second object is proxified automatically by Hibernate. But both object are initialized (Hibernate.initialize and Hibernate.isPropertyInitialized returns true) and have their properties fully available. The only sign of this second object not being real object is it's classname. Instead of proper classname it returns proxified name of it's abstract ancessor. So object2.getClass().getName() returns something like AbstractPersonObject_$$_javassist_770 instead of Worker classname.
This behaviour has appeared after upgrading Hibernate from 3.2.6 to 3.3.2.
I don't understand it - if object is initialized, proxy should be abandoned, right?


Top
 Profile  
 
 Post subject: Re: Unnecessary Proxy?
PostPosted: Fri Jan 15, 2010 9:13 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
>> if object is initialized, proxy should be abandoned, right?

No, once a determinate object is initialized as proxy then it will be returned as proxy during the whole persistent context lifetime.

Following scenario explains why:

1.) A a1 = (A) session.load(A.class,id); // loads instance a as prox
2.) System.out.println(a1.getName()) // here the real object is accessed
Peter
3.) A a2 = (A) session.createQuery("from A where name='Peter'").uniqueResult();

If you now expect a2 being de-proxied, then a1 == a2 would be false although referring the same db-record.
This goes in contradiction to what is specified in
http://docs.jboss.org/hibernate/stable/ ... s-identity

In other words:
To maintain the object identity contract within a transaction, Hibernate tries to expose to the
programmer a determinate persistent instance either always as proxy or always as real object,
it depends only from the way how it is accessed for the first time in the transaction.


Top
 Profile  
 
 Post subject: Re: Unnecessary Proxy?
PostPosted: Fri Jan 15, 2010 9:20 am 
Newbie

Joined: Thu Feb 28, 2008 4:39 am
Posts: 4
So how can I read classname from proxified object? Is there some hibernate utility method to do that?


Top
 Profile  
 
 Post subject: Re: Unnecessary Proxy?
PostPosted: Fri Jan 15, 2010 10:14 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
if ( a instanceof HibernateProxy) {
Object noproxy = ((HibernateProxy) a).getHibernateLazyInitializer().getImplementation();
noproxy.getClass();
}

This solution returns you the concrete class of your persistent object,
but on the other hand it produces a db-hit if the proxy was not initialized yet.

The other solution is:

Class<?> cls2 = HibernateProxyHelper.getClassWithoutInitializingProxy(a);

It returns you the class to which a belongs without initializing the proxy.
The behavior of the ladder is slightly different if you use proxies in combination with polymorphic assoctiations:
in such case HibernateProxyHelper.getClassWithoutInitializingProxy(a) may return the superclass of a and not necessarily the concrete class of a.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.