This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Problem in adding new child to a parent - LazyInitializationException

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"KK"

Please check your private message, for a message from JavaRanch.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry its DEPT_NUMBER in the Dept.hbm.xml.
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic