-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problems with PersistentSet
PostPosted: Wed Jun 28, 2006 10:06 am 
Newbie

Joined: Thu Apr 06, 2006 4:01 pm
Posts: 7
Hi all,

Is there any way to not use PersistentSet in set mappings ?

My setter method has to iterate over the set, so when Hibernate call the method, an LazyInitializationException is thrown.

The Exception:
12:59:25,832 ERROR [LazyInitializationException] illegal access to loading collection
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:138)
at java.util.AbstractCollection.addAll(AbstractCollection.java:316)

The method:
Code:
   @SuppressWarnings("unused") private void setInterfaceAttributeList(Set<InterfaceAttribute> interfaceAttributeList) {
      this.interfaceAttributeList = new LinkedHashSet<InterfaceAttribute>();
      if (interfaceAttributeList != null)
         this.interfaceAttributeList.addAll(interfaceAttributeList);
   }


tks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 2:20 pm 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
I believe PersistentSet maintains iteration order constant -- it behaves just like a LinkedHashSet, so I suggest you declare the property as a java.util.Set and just assume iteration order is constant.
If you are going to close the session before iterating over this lazy collection just call size() on the collection before closing the session.

_________________
Henrique Sousa
Don't forget to rate useful responses


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 5:27 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, in fact there's no point doing it...

Where are you receiving the LazyInitializationException ? Did you close the Hibernate Session before trying to access the collection ?

You should read about the N+1 syndrom with mapping O/R. Typically, here you say that you want to access the collection. You have to understand the principles behind lazy loading.

In fact, lazy loading is quite simple. It's using kind of a java proxy : instead of doing things directly, something is put between. Concerning persisitent collections, even if you weren't using PersistentSet, you'd meet the problem : you get the exception because you're trying to access data which hasn't been loaded yet, while the Hibernate Session is closed. You should iterate once on the collection before this session is closed not to get the exception anymore (that's why hlfsousa tells you to call size() : same result => to know the set size, the set has to be completely iterated over, so it's also wholy loaded).

To illustrate this, and THIS IS Very important when you develop with Hibernate : activate the display of the SQL queries sent to the db (show_sql => true). If you do this, you'll see that iterating on your collection (before session is closed, obviously) triggers a select for each element of the collection.

Is it a bit clearer with this explanation ? Do you have other questions about this ?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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