Hibernate is not working as per the documentation, i am using something like
Hibernate: select section0_.sectionid as sectionid0_, section0_.sectionname as sectionn2_0_ from section section0_ where section0_.sectionid=?
Hibernate: select subjects0_.subjectid as subjectid__, subjects0_.subjectid as subjectid1_, subjects0_.subjectname as subjectn2_1_, subjects0_.sectionid as sectionid1_, section1_.sectionid as sectionid0_, section1_.sectionname as sectionn2_0_ from subject subjects0_ left outer join section section1_ on subjects0_.sectionid=section1_.sectionid where subjects0_.subjectid=?
Hibernate: update subject set subjectname=?, sectionid=? where subjectid=?
Hibernate version: 2.1
Mapping documents:
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
Code between sessionFactory.openSession() and session.close():
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();
_______________
works correctly. why should i write save to question explicitly.
Full stack trace of any exception that occurs:
row not found,
Name and version of the database you are using:
MS Sql 2000
The generated SQL (show_sql=true):
Hibernate: select section0_.sectionid as sectionid0_, section0_.sectionname as sectionn2_0_ from section section0_ where section0_.sectionid=?
Hibernate: select subjects0_.subjectid as subjectid__, subjects0_.subjectid as subjectid1_, subjects0_.subjectname as subjectn2_1_, subjects0_.sectionid as sectionid1_, section1_.sectionid as sectionid0_, section1_.sectionname as sectionn2_0_ from subject subjects0_ left outer join section section1_ on subjects0_.sectionid=section1_.sectionid where subjects0_.subjectid=?
Hibernate: update subject set subjectname=?, sectionid=? where subjectid=?
|