-->
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.  [ 4 posts ] 
Author Message
 Post subject: Urgent - Saving Ilist cascade fails
PostPosted: Wed Sep 21, 2005 4:47 am 
Newbie

Joined: Mon Sep 19, 2005 5:45 am
Posts: 6
Hi , when i try to save an object that have an Ilist with the attribute cascade=all i got this error:

Another object was associated with this id ( the object with the given id was already loaded): [Middleware.Exercises.SubmitQuestion#70]

i already try the 2 methods Save Update and SaveOrUpdate

Do you know what was wrong?

One more thing that can help you help me, i have de unsaved-value="0" in the mapping file for SubmitQuestions, and before i call the SaveOrUpdate method i give the SubmitQuestionID value, so he is different than 0, so i dont understant why i got this error.

Another object was associated with this id ( the object with the given id was already loaded): [Middleware.Exercises.SubmitQuestion#70]
One mor time thanks for you help.

Hugo Alves


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 6:11 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
Please post a clear question with sample mapping files and code.
Form what you describe you are setting yourself the id for your object? this is bad in most situations, depending on you mappings.

Regards,
Radu


Top
 Profile  
 
 Post subject: IList save method fails
PostPosted: Fri Sep 23, 2005 6:35 am 
Newbie

Joined: Mon Sep 19, 2005 5:45 am
Posts: 6
Hi, i'm going to try explain my problem again.
I have an object for the class SubmitExercise with this mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Middleware.Exercises.SubmitExercise, Middleware" table="Elearn_SubmitExercises">
<id name="SubmitExerciseID" column="SubmitExerciseID" type="Int32" length="4" unsaved-value="0">
<generator class="identity"/>
</id>

<property name="Late" column="Late" type="Boolean" length="1"/>
<property name="DateStart" column="DateStart" type="DateTime" length="8"/>
<property name="DateFinish" column="DateFinish" type="DateTime" length="8"/>
<property name="Score" column="Score" type="Single" length="9"/>

<list name="SubmitQuestions" table="Elearn_SubmitQuestions" cascade="all">
<key column="SubmitExerciseID"/>
<index column="Indice"/>
<one-to-many class="Middleware.Exercises.SubmitQuestion, Middleware"/ </list>

</class>

</hibernate-mapping>


That represents an exercise that have a list of submited questions by the students.

Tha mapping file for the submitQuestion is :

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

<class name="Middleware.Exercises.SubmitQuestion, Middleware" table="ELearn_SubmitQuestions">
<id name="SubmitQuestionID" column="SubmitQuestionID" type="Int32" length="4" unsaved-value="0">
<generator class="identity"/>
</id>

<property name="Grade" column="Grade" type="Single" length="9"/>
<property name="SubmitExerciseID" column="SubmitExerciseID" type="Int32" length="4"/>
<property name="QuestionID" column="QuestionID" type="Int32" length="4"/>

<one-to-one name="Answer" class="Middleware.Exercises.SubmitAnswer, Middleware" cascade="all"/>

</class>

</hibernate-mapping>


And finnaly i have a mapping file for the SubmitAnswer class that have 2 types a simple TextAnswer or a SubmitChoiceAnswer and have this mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

<class name="Middleware.Exercises.SubmitAnswer, Middleware" table="Elearn_SubmitAnswer">
<id name="SubmitAnswerID" column="SubmitAnswerID" type="Int32" length="4" unsaved-value="0">
<generator class="foreign">
<param name="property">SubmitQuestion</param>
</generator>
</id>

<one-to-one name="SubmitQuestion" class="Middleware.Exercises.SubmitQuestion, Middleware" constrained="true"/>

<joined-subclass name="Middleware.Exercises.SubmitTextAnswer, Middleware" table="Elearn_SubmitTextAnswer">
<key column="SubmitAnswerID"/>
<property name="TextAnswer" column="Text" type="string"/>
</joined-subclass>

<joined-subclass name="Middleware.Exercises.SubmitChoiceAnswer, Middleware" table="Elearn_SubmitChoiceAnswer">
<key column="SubmitAnswerID"/>
<list name="ChoiceAnswer" table="Elearn_SubmitChoiceAnswer2" cascade="all">
<key column="SubmitChoiceAnswerID"/>
<index column="ChoiceOrder"/>
<composite-element class="Middleware.Exercises.AnswerOption, Middleware">
<property name="Text" column="Text" type="string"/>
<property name="Valid" column="Valid" type="boolean"/>
</composite-element>
</list>
</joined-subclass>
</class>

So when i try so save an object SubmitExercise i do that:

I read de object from de session and pass de ID:

SubmitExercise exameLido = (SubmitExercise)session.Load(typeof(SubmitExercise), ID);

then I set the property SubmitQuestions equal to the questions that the user post like this:

exameLido.SubmitQuestions=(IList)questoes;
session.SaveOrUpdate(exameLido);
transaction.Commit();
session.Close();

and i got the error that i describe in de first post.

If you want more information take a look at the post "Urgent - List Mapping problem"

Thanks for your help.

Hugo Alves


</hibernate-mapping>
[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 7:30 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
well, you have a pretty complex hierarchy...
First, you have a design problem:
Don't define the foreign keys as properties of your objects!

In the database you use foreign keys to define relationships, but
in object world you have associations between objects, so in the SubmitQuestion mapping the SubmitExerciseID property should be replaced with a many-to-one to the SubmitExercise object. In this way you define a bidirectional association, so you must declare the SubmitQuestions list as inverse=true. in the code sample you didn't explain how do you create the "questoes" list. So start by cleaning up your properties and define proper associations between objects.

Regards,
Radu


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

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.