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