-->
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.  [ 6 posts ] 
Author Message
 Post subject: Urgent - List mapping problem
PostPosted: Mon Sep 19, 2005 5:58 am 
Newbie

Joined: Mon Sep 19, 2005 5:45 am
Posts: 6
Hi people, i have a problem with this mapping file that do not allow me to save un object, my mapping file is this:

<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="DateStart" column="DateStart" type="DateTime" length="8"/>
<property name="DateFinish" column="DateFinish" type="DateTime" length="8"/>

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

</hibernate-mapping>


that maps de class submitExercise that have a list of submited questions.
I have 2 tables one submiteExercises and another SubmitQuestions, this last one have this fields:

SubmitQuestionID int (Primary key and identity)
SubmitExerciseID int
QuestionID int
Indice int
Grade decimal

and my submitexercise table have this fields:

SubmitExerciseID int
DateStart datetime 8 1
DateFinish datetime 8 1

my problem is when, that first when i log in as a teacher i create a record for each student in table submitexercise, and than when i log in as student and try to solve de exercise i have got this error:

could not synchronize database state with session
Exception: NHibernate.TransientObjectException
Message: object references an unsaved transient instance - save the transient instance before flushing: SubmitQuestion
Source: NHibernate
at NHibernate.Impl.SessionImpl.ThrowTransientObjectException(Object obj)
at NHibernate.Impl.SessionImpl.GetEntityIdentifierIfNotUnsaved(Object obj)
at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand cmd, Object value, Int32 index, ISessionImplementor session)
at NHibernate.Collection.CollectionPersister.WriteElement(IDbCommand st, Object elt, Boolean writeOrder, ISessionImplementor session)
at NHibernate.Collection.List.WriteTo(IDbCommand st, CollectionPersister persister, Object entry, Int32 i, Boolean writeOrder)
at NHibernate.Collection.CollectionPersister.InsertRows(PersistentCollection collection, Object id, ISessionImplementor session)
at NHibernate.Impl.ScheduledCollectionUpdate.Execute()
at NHibernate.Impl.SessionImpl.ExecuteAll(ICollection coll)
at NHibernate.Impl.SessionImpl.Execute()


Please someone help me, this is the only problem i have to finish my project.

Thanks

Hugo Alves


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 7:23 am 
Regular
Regular

Joined: Fri Jun 11, 2004 6:27 am
Posts: 81
Location: Yaroslavl, Russia
Try to add cascade="all" attribute to the <list> element.

_________________
Best,
Andrew Mayorov // BYTE-force


Top
 Profile  
 
 Post subject: IList Mapping problem
PostPosted: Tue Sep 20, 2005 7:38 am 
Newbie

Joined: Mon Sep 19, 2005 5:45 am
Posts: 6
Hi again, thanks for you help it really solve my problem, but now i have another one.
When I update for de first time de list of questions the nhibernate Insert them on the respective table, the problem is that when i want to update them it always does an insert and not an update, i think the problem maybe on the unsaved-value=0 or in my save code that is somethins like that:
SubmitExercise exame=(SubmitExercise) LoadExameSubmetido(exameID);

thats load de exercise from the database and then i create a new arraylist tha contain objects of type submitQuestions and then i do that:

exame.SubmitQuestion=(IList)questions;

and call the save method of nhibernate.

my mapping file for the submitquestions is this:

<?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 the answer mapping file is this:

<?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>

</hibernate-mapping>


With this code an this mapping files he always makes inserts of the object that are store in the class SubmitQuestion (the answers) and not update them, and in the final it trys to make a stupid update to the table SubmitQuestions like this:

UPDATE SubmitQuestions SET SubmitExerciseID = null, Indice = null WHERE SubmitExerciseID = @p0', N'@p0 int', @p0 = 4

and i dont know why.

Please help again.

Tanks.

Hugo Alves


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 20, 2005 8:18 am 
Regular
Regular

Joined: Fri Jun 11, 2004 6:27 am
Posts: 81
Location: Yaroslavl, Russia
You should use Save method for inserting of a record and Update for updating. You can also use SaveOrUpdate method, which will either insert or update a record according to, for example, unsaved-value attribute.

_________________
Best,
Andrew Mayorov // BYTE-force


Top
 Profile  
 
 Post subject: IList mapping problem
PostPosted: Tue Sep 20, 2005 9:28 am 
Newbie

Joined: Mon Sep 19, 2005 5:45 am
Posts: 6
Hi again, one more time thanks for your help, i understand your explication, and them a change my save function so SaveOrUpdate, but i still cant update de questions i got this error:

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

Do you know what was wrong?

Thanks.

Hugo Alves


Top
 Profile  
 
 Post subject: IList mapping problem
PostPosted: Tue Sep 20, 2005 9:33 am 
Newbie

Joined: Mon Sep 19, 2005 5:45 am
Posts: 6
Hi again 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.

One mor time thanks for you help.

Hugo Alves


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.