*
The moose likes Object Relational Mapping and the fly likes NonUniqueObjectException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "NonUniqueObjectException" Watch "NonUniqueObjectException" New topic
Author

NonUniqueObjectException

sura watthana
Ranch Hand

Joined: Sep 13, 2004
Posts: 77
Hi

I am new to Hibernate. I am trying to implement a many-to-many association.
I have a simple scenario where I have two entities three tables.
one is Teacher.java with teacher table containg id and name.
the other is Student.java with student table containing id and name as well.

let me show you my java files, my tables and the hibernate mapping files.





<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.oxman.entity.Teacher" table="teachers">
<id name="id" column="uid" type="int" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" type="string" length="255"/>
<set name="students" table="teachers_students" cascade="all">
<key column="teacher_id"/>
<many-to-many class="com.oxman.entity.Student"/>
</set>
</class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.oxman.entity.Student" table="students">
<id name="id" column="uid" type="int" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" type="string" length="255"/>
<set name="teachers" table="teachers_students" cascade="all">
<key column="student_id"/>
<many-to-many class="com.oxman.entity.Teacher"/>
</set>
</class>
</hibernate-mapping>



DROP TABLE IF EXISTS `teachers`;
CREATE TABLE `teachers` (
`uid` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `students`;
CREATE TABLE `students` (
`uid` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `teachers_students`;
CREATE TABLE `teachers_students` (
`teacher_id` int(11) NOT NULL default '0',
`student_id` int(11) NOT NULL default '0',
PRIMARY KEY (`teacher_id`,`student_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



I have tried to run this code

Here is the Exception stack trace

Hibernate: insert into teachers (name) values (?)
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.oxman.entity.Student#0]
at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:590)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at com.oxman.test.StudentTeacherTest.main(StudentTeacherTest.java:36)



Does anybody have any idea what went wrong?

thank you

Sura
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Please do not post questions pertaining to this same code more than once.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NonUniqueObjectException
 
Similar Threads
elt column hibernate
Mapping Error Hibernate
many side of one to many not being populated
org.hibernate.PropertyAccessException
StaleStateException Hibernate