-->
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: merging an object with a set of composite elements
PostPosted: Thu Apr 05, 2007 12:29 pm 
Beginner
Beginner

Joined: Fri May 12, 2006 9:58 am
Posts: 32
Hibernate version:
3.2

Mapping documents:
Code:
<class name="com.foo.Page" table="pages">

   <set name="logs" table="page_logs">
      <key column="page_id" />
      <composite-element class="com.foo.Log">
         <property name="ip" />
         <property name="action" />
      </composite-element>
   </set>

</class>

Code:
Code:
1.  // continue edit-style-use-case
2.  // get detached page from http session
3.  Page p = getPageFromHttpSession();
4. 
5.  // add a Log
6.  p.getLogs().add(new Log("host", "example"));
7. 
8.  // start session
9.  sessionFactory.openSession();
10.
11. // merge page on new session
12. session.merge(p);


Full stack trace of any exception that occurs:
Code:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.foo.Page.logs, no session or session was closed
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
   at org.hibernate.collection.PersistentSet.add(PersistentSet.java:189)
...


Name and version of the database you are using:
MySQL 5.xx


Hello

I have a Page object with a Set<Log> logs mapped as composite element. I can CRUD this Page with it's logs fine. The problem appears when I try to merge an existing Page, taken from the http session, back to the new hibernate session. If any changes have been applied on the logs set, I get the exception shown above.

I managed to fix this by placing lazy="false" in the <set> mapping. It does the job, but now I get extra SQL statements every time I access any Page even though I do not touch the logs Set.

The exception says "no session or session was closed", but if I remove line 6 (which alters the logs set) it works fine. So I get the impression that it's not the session's problem but something else.

Any ideas on what should I do?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 05, 2007 1:08 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Do you have a version or timestamp column in your mappings? If I recall correctly, merge() will not properly handle modifications to detached objects without some form of optimistic locking.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 05, 2007 1:41 pm 
Beginner
Beginner

Joined: Fri May 12, 2006 9:58 am
Posts: 32
Yes I do have a <version name="version" type="long" /> in the Page Object. I just did not show it in the simplified example. CRUD, optimistic locking and merging works properly. I just have problem when merging and the Pages.logs Set has been modified.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 05, 2007 2:03 pm 
Beginner
Beginner

Joined: Fri May 12, 2006 9:58 am
Posts: 32
OK. Problem solved. I read 19.1.4. Initializing collections and proxies

I had to do a Hibernate.initialize(p.getLogs()) to initialize the lazy loaded collection before closing the session.

Thanks


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.