These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: NonUniqueObjectException in One-to-Many-Association
PostPosted: Tue Feb 01, 2005 1:37 pm 
Regular
Regular

Joined: Thu Feb 19, 2004 4:48 am
Posts: 62
Hi,

I have a bidirectional One-to-Many-Association between Question and Answer- the mapping works fine for existing Questions/Answers, e.g.:

Question q = (Question)session.load(Question.class, 9);
Answer a = (Answer)session.load(Answer.class, 3);
question.addAnswer(a1);
question.addAnswer(a2);
Transaction tx = session.beginTransaction();
session.save(question);
tx.commit();

works as expected and properly save the whole tree.

However, when I do the same with freshly created questions/answers:

Question q = new Question();
Answer a1 = new Answer();
Answer a2 = new Answer();

question.addAnswer(a1);
question.addAnswer(a2);
Transaction tx = session.beginTransaction();
session.save(question);
tx.commit();

I get a

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [de.vierundsechzig.springer.model.modules.quiz.Answer#0]

I understand, that the reason for this is the fact that the answers primary keys are 0 before the actual create, which leads to duplicate answer-entries in the session - but how am I supposed to set hibernate up to get arround this problem? I tried several Cascade- and fetch-Types, but I doubt that this will solve the problem...

I also tried to save the Classes in different sessions, but the id's are not filled after the save, so, unless I always find another unique identifier in the class, i have no way to determine the id's of the classes I just saved - therefore I cannot retrieve them again...

In case this is dependent on the setup (I doubt...), I use:
-hibernate3.0b3
-hibernate annotations3.0a2,
-mysql 4.0.15-nt (using auto_increment for id-generation)
-jdk1.5.0
-win xp

thx for any help!
stefan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.