-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to remove proxies
PostPosted: Tue May 09, 2006 1:24 pm 
Newbie

Joined: Wed Nov 30, 2005 11:13 am
Posts: 10
Hibernate version: 3.0.5

Hi,

I am working on a multi-tier application using Hibernate and so far everything was working nicely. Now I am facing a little problem: My objects are in a certain state - partially initialized - and now I want to:

a) use JAXB/JiBX (yes, I've looked at HyperJaxb) to marshal those objects and exclude all uninitialized collections/associations.
b) pass my objects to my view layer (JSF) without having to worry about LazyInitializationExceptions, and yes, I've looked at OSIV but decided not to use it, because I'd rather use my business layer to fetch everything that is required.

After doing some searching, I believe it is possible to unproxy detached objects but unfortunately I have not found any hints on how to actually achieve this. AFAIK, the only other solution would be to use DTOs, which I want to avoid.

Can anybody please provide some information and/or links on how to achive this?

Regards,
Tom


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 2:29 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
Something like this should do it:-


Code:
if (proxy instanceof HibernateProxy){
   return ((HibernateProxy)proxy).getHibernateLazyInitializer().getImplementation();
}


Hope that helps.
Cheers,
Rich.


Top
 Profile  
 
 Post subject: One step back
PostPosted: Wed May 10, 2006 4:27 am 
Newbie

Joined: Wed Nov 30, 2005 11:13 am
Posts: 10
Hi,

thanks for the answer - unfortunately it did not solve my problem yet - but maybe that's just because my understanding of how hibernate actually works is wrong. Here is what I am doing:

a) load a object with lazy associations/collections via session.get(), or
b) load a number of lazy objecs with using a HQL query.

In both cases, the lazy associations are not initialized. I am currently assuming the following;
1) as soon as I close the session, my objects are detached. Accessing any unintialized collection/association will result in a LazyInitializationException.
2) if I need to access these collections, I can do so by reattaching the object to a session and then call Hibernate.initialize() where necessary.

Basically my question is now, are uninitialized collections in my objects proxies? If so, can I unproxy them in the way mentioned in the previous post and if they are not proxies, what are they and how can I treat them? I've looked at the hibernate source code and AFAIU, my collections (sets) are actually PersistentSets, which are linked to a session. If the original session is closed, I figure that the persistent set is actually useless unless you reconnect to a new session.

Regards,
Tom


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 4:42 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
Yes - as I understand it that is correct. Collections are not proxies but are implemented using the org.hibernate.collection.Persistent* classes. If they are not initialised before the session is closed then they are basically useless until setCurrentSession is called with a valid session.


Top
 Profile  
 
 Post subject: Where to got from here?
PostPosted: Wed May 10, 2006 5:22 am 
Newbie

Joined: Wed Nov 30, 2005 11:13 am
Posts: 10
Ok, but now my problem is this, if I access the uninitialized collection, it is not null but upon iterating over its children, of course a LazyInitializationException is thrown. So basically form the outside, it appears as if the collection is available when this is actually not the case. This in turn leads to my question: how can I distinguish between uninitialized and initialized collections?

Regards,
Tom


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 5:43 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
The static utility method org.hibernate.Hibernate.isInitialized(Object) should work for both proxies and persistent collections. In fact, it should give a sensible result for anything as passing anything else in returns true too.


Top
 Profile  
 
 Post subject: Solved
PostPosted: Fri May 19, 2006 4:21 am 
Newbie

Joined: Wed Nov 30, 2005 11:13 am
Posts: 10
It's working now - here is what we did:
1) added method to AbstractPersistentCollection to check transaction state
2) AspectJ around advice for all collections and one-to-one associations
3) AOP uses Hibernate.isInitialized() to determine state of the association
4) If Hibernate.isInitialized() returns true, we expose the collection, otherwise we return null

By doing this, we can use JAXB/JIBX to marshal our object graph only to a certain extent and we do not have to worry about LazyInitializationExceptions any more - now we have NPEs :o)

Regards,
Tom


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