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.  [ 5 posts ] 
Author Message
 Post subject: Determine whether an association has already been loaded
PostPosted: Thu Mar 18, 2010 12:22 pm 
Newbie

Joined: Thu Mar 18, 2010 12:02 pm
Posts: 2
Hi,
I have a simple model with two entities, let's say Customer and address. The address association is set to "Lazy=true" in the mappings. After the customer has been fetched, in a certain place in the code, I need to determine whether the address has already been fetched or not.
Is there a way to find out?

Thanks,
Avi


Top
 Profile  
 
 Post subject: Re: Determine whether an association has already been loaded
PostPosted: Fri Mar 19, 2010 7:31 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Code:
java.util.Collection collection = customer.getAddresses();
if (collection instanceof  org.hibernate.collection.PersistentCollection) {// is usually not true on new objects created in transaction
       boolean isloaded  = ((org.hibernate.collection.PersistentCollection) collection).wasInitialized();
}



This should work in the case of a ...ToMany relation.


Top
 Profile  
 
 Post subject: Re: Determine whether an association has already been loaded
PostPosted: Fri Mar 19, 2010 7:33 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Oh, I oversight that here we are in NHibernate not in Hibernate forum.
Anyway I hope there exist analow apis in NHibernate.


Top
 Profile  
 
 Post subject: Re: Determine whether an association has already been loaded
PostPosted: Sun Mar 21, 2010 5:23 am 
Newbie

Joined: Thu Mar 18, 2010 12:02 pm
Posts: 2
Hi,
Thanks for the replay. Although it is relevant to Hibernate, it is very much like it supposed to be in NHibernate:

((NHibernate.Collection.PersistentSet)MyCollection).WasInitialized

Maybe you know what to do in case that my association isn’t a collection. Let's say that every customer has only one address (the address is a different class that located on other table than the customer)

Thanks,
Avi


Top
 Profile  
 
 Post subject: Re: Determine whether an association has already been loaded
PostPosted: Mon Mar 22, 2010 3:33 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Again, I can only tell how I would do it in Hibernate:

Code:
boolean initialized = true; 
if (customer instanceof org.hibernate.proxy.HibernateProxy) {
       initialized = ! ((org.hibernate.proxy.HibernateProxy) customer).getHibernateLazyInitializer().isUninitialized();
}
// else initialized = true;  non-proxy references are always loaded


I hope there exist analog api in NHibernate.


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