• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in adding new child to a parent - LazyInitializationException

 
Kanag sen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"KK"

Please check your private message, for a message from JavaRanch.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry its DEPT_NUMBER in the Dept.hbm.xml.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic