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
|