IntelliJ Java IDE
The moose likes Object Relational Mapping and the fly likes Problem in adding new child to a parent - LazyInitializationException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Problem in adding new child to a parent - LazyInitializationException" Watch "Problem in adding new child to a parent - LazyInitializationException" New topic
Author

Problem in adding new child to a parent - LazyInitializationException

Kanag sen
Greenhorn

Joined: Sep 07, 2003
Posts: 8
Hi
I have a Dept table with one-to-many relation with employee table and employee table with many-to-one relation with Dept table. When I tried to map two employees to the same Dept I got "NonUniqueObjectException". So I changed the hibernate mapping file from lazy="false" to lazy="true" in both the Dept and Employee hbm.xml files. This solved my problem.

But now when I try to add a new Employee to a Dept which is already assosiated with the other Employees, I got "LazyInitializationException". I know its because of the lazy="true". But when I make it to false then my previous issue will not be fixed. I do not know what to do.

It would be really great if someone could help me.


=============<br />SCJCP, SCWCD
Jaikiran Pai
Saloon Keeper

Joined: Jul 20, 2005
Posts: 6718

"KK"

Please check your private message, for a message from JavaRanch.


[My Blog] [JavaRanch Journal]
Jaikiran Pai
Saloon Keeper

Joined: Jul 20, 2005
Posts: 6718

Thanks for changing your display name

As for your question, a NonUniqueObjectException is not because of setting lazy=true (or false). Can you post your mapping files and also the code where you are adding the child to the parent. Also please post the entire exception stacktrace which contains this error message
Kanag sen
Greenhorn

Joined: Sep 07, 2003
Posts: 8
Hi
Here are the details.Thank you for helping me.

Employee.hbm.xml
----------------

<many-to-one name="depts" class="com.payroll.biz.model.Dept" lazy="true" >
<meta attribute="field-description">
@hibernate.many-to-one not-null="true" @hibernate.column name="DEPT_NUMBER"
</meta>
<column name="DEPT_NUMBER"/>
</many-to-one>


Dept.hbm.xml
------------

<set name="employees" lazy="true" inverse="true" cascade="all">
<meta attribute="field-description">
@hibernate.set lazy="true" inverse="true" cascade="all"
@hibernate.collection-key column="EMPLOYEE_NUMBER"
@hibernate.collection-one-to-many class="com.payroll.biz.model.Employee"
</meta>
<key>
<column name="EMPLOYEE_NUMBER"/>
</key>
<one-to-many class="com.payroll.biz.model.Employee"/>
</set>

Exception
---------

2007-07-27 12:32:49,992-http-7070-Processor23 (ERROR) LazyInitializationException.<init>(19) - failed to lazily initialize a collection of role: com.threeht.iriswi.biz.model.LineItem.asids - no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.threeht.iriswi.biz.model.LineItem.asids - no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:191)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:183)
at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:61)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:158)


Thanks
kk
Kanag sen
Greenhorn

Joined: Sep 07, 2003
Posts: 8
Sorry its DEPT_NUMBER in the Dept.hbm.xml.
 
 
subject: Problem in adding new child to a parent - LazyInitializationException
 
Threads others viewed
LazyInitializationException
LazyInitializationException
Another LazyInitializationException: illegal access to loading collection
LazyInitializationException
LazyInitializationException
IntelliJ Java IDE