-->
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.  [ 3 posts ] 
Author Message
 Post subject: Parent Child relations
PostPosted: Sat Feb 26, 2005 4:04 am 
Newbie

Joined: Sat Feb 26, 2005 3:42 am
Posts: 3
I have two classes

Subject
Question

Question.hbm.xml

<class name="Question" table="Question">
<id name="questionid" column="questionID" type="string"
unsaved-value="null">
<generator class="assigned" />
</id>
<property name="questiondescription"
column="questionDescription" type="java.lang.String" />
<property name="option1" column="option1"
type="java.lang.String" />
<property name="option2" column="option2"
type="java.lang.String" />
<property name="option3" column="option3"
type="java.lang.String" />
<property name="option4" column="option4"
type="java.lang.String" />

<many-to-one name="subject" column="subjectID"
class="Subject"
not-null="true">
</many-to-one>

</class>

Subject.hbm.xml
<class name="Subject" table="Subject">
<id name="subjectid" column="subjectID" type="string"
unsaved-value="null">
<generator class="assigned" />
</id>
<property name="subjectname" column="subjectName"
type="java.lang.String" />
<set name="questions" inverse="true"
cascade="all-delete-orphan">
<key column="subjectid"></key>
<one-to-many class="Question" />
</set>
</class>

Table Structure


Subject
----------
subjectID varchar
subjectName varchar

Question
----------
All fields are varchar
-----------------------
questionID
questionDescription
subjectID FK_SUBJECT_QUESTION
option1
option2
option3
option4

Classes contain usual geter-setter except
Subject has following,
getQuestions();
setQuestions();

addQuestion(Question q){
q.setSubject(this);
getQuestions().add(q);
}

Problem is
Question q = new Question(,,,);
q.setters are set

begintransaction
sub = (Subject) session.load(Subject.class, "TS12");
sub.addQuestion(q);
commit or flush();

this doesn't work, but
__________________
Question q = new Question(,,,);
q.setters are set

begintransaction
sub = (Subject) session.load(Subject.class, "TS12");
sub.addQuestion(q);
session.save(q);
commit or flush();
_______________
work correctly? where am i wrong?

Thanks
Nirav


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 26, 2005 11:52 am 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
Looks like you have to saveOrUpdate the new entity Question to get it persisted, and stored in the Subject. I believe this is the documented behavior.
Is it causing a problem to use the second, working technique?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 1:25 am 
Newbie

Joined: Sat Feb 26, 2005 3:42 am
Posts: 3
mgreer wrote:
Looks like you have to saveOrUpdate the new entity Question to get it persisted, and stored in the Subject. I believe this is the documented behavior.
Is it causing a problem to use the second, working technique?


Documentation doesn't tell that you have to persist Child explicitly if you are using inverse="true" and cascade="all", my problem is i don't want to write
session.save(OrUpdate)(question),

This should work according to docs.

Question q = new Question();
q.setters are set

Subject s = load existing

s.addQuestion(q);
flush or commit

Regards and Thanks
Nirav


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