I have two tables (Test, Question)
--------------------
Test: id, name
Question: id, content
--------------------
Each test has many questions in Question, each question could belong to multiple tests in Test.
I use a third table TestQuestionRelation (tid, qid) to connect the two. I've created two classes for Test and Question.
Here are the details:
HBMs
===
Test.hbm.xml
<pre>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";>
<hibernate-mapping>
<class
name="hibernate.Test"
table="test"
proxy="hibernate.Test"
dynamic-update="false"
dynamic-insert="false"
mutable="true"
>
<id
name="id"
column="id"
type="long"
unsaved-value="null"
>
<generator class="native">
</generator>
</id>
<property
name="title"
type="java.lang.String"
update="true"
insert="true"
column="title"
not-null="false"
unique="true"
/>
<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="description"
not-null="false"
unique="false"
/>
<property
name="entrydate"
type="java.util.Date"
update="true"
insert="true"
column="entrydate"
not-null="false"
unique="false"
/>
<set
name="candidates"
table="TestEmployeeRelationEnum"
lazy="false"
inverse="false"
cascade="none"
sort="unsorted"
>
<key
column="tid"
/>
<index
column="tid"
/>
<many-to-many
class="hibernate.Employee"
column="eid"
outer-join="auto"
/>
</set>
<set
name="objectiveQuestions"
table="testObjectiveQuestionRelation"
lazy="false"
inverse="false"
cascade="none"
sort="unsorted"
>
<key
column="tid"
/>
<index
column="tid"
/>
<many-to-many
class="hibernate.QuestionObjective"
column="qid"
outer-join="auto"
/>
</set>
<set
name="subjectiveQuestions"
table="testSubjectiveQuestionRelation"
lazy="false"
inverse="false"
cascade="none"
sort="unsorted"
>
<key
column="tid"
/>
<index
column="tid"
/>
<many-to-many
class="hibernate.QuestionSubjective"
column="qid"
outer-join="auto"
/>
</set>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Test.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
</pre>
QuestionObjective.hbm.xml
<pre>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";>
<hibernate-mapping>
<class
name="hibernate.QuestionObjective"
table="objectiveQuestion"
proxy="hibernate.QuestionObjective"
dynamic-update="false"
dynamic-insert="false"
mutable="true"
>
<id
name="id"
column="id"
type="long"
unsaved-value="null"
>
<generator class="native">
</generator>
</id>
<property
name="content"
type="java.lang.String"
update="true"
insert="true"
column="content"
not-null="false"
unique="false"
/>
<property
name="filename"
type="java.lang.String"
update="true"
insert="true"
column="filename"
not-null="false"
unique="false"
/>
<property
name="instruction"
type="java.lang.String"
update="true"
insert="true"
column="instruction"
not-null="false"
unique="false"
/>
<property
name="correctAnswer"
type="java.lang.String"
update="true"
insert="true"
column="correctanswer"
not-null="false"
unique="false"
/>
<many-to-one
name="catalog"
class="hibernate.Catalog"
cascade="none"
outer-join="false"
update="true"
insert="true"
column="cid"
unique="false"
/>
<many-to-one
name="type"
class="hibernate.QuestionType"
cascade="none"
outer-join="false"
update="true"
insert="true"
column="typeid"
unique="false"
/>
<many-to-one
name="level"
class="hibernate.HardLevel"
cascade="none"
outer-join="false"
update="true"
insert="true"
column="lid"
unique="false"
/>
<property
name="hits"
type="int"
update="true"
insert="true"
column="hits"
not-null="false"
unique="false"
/>
<set
name="tests"
table="testObjectiveQuestionRelation"
lazy="false"
inverse="false"
cascade="none"
sort="unsorted"
>
<key
column="qid"
/>
<index
column="qid"
/>
<many-to-many
class="hibernate.Test"
column="tid"
outer-join="auto"
/>
</set>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-QuestionObjective.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
</pre>
Must I write a class for table testObjectiveQuestionRelation ?
In RDB, I can easily write a SQL to get all the question Ids of a certain test, such as this :
select qid from testObjectiveQuestionRelation where tid=?
With some help I write an HQL like the following:
"select q.id from " +
" hibernate.QuestionObjective as q inner join q.tests as t " +
" where t.id=:id
But the SQL generated by hibernate doesn't seem as clean as the above one:
Hibernate: select question0_.id as x0_0_ from objectiveQuestion question0_ inner
join testObjectiveQuestionRelation tests1_ on question0_.id=tests1_.qid inner join test test2_ on tests1_.tid=test2_.id where (test2_.id=? )
Any help is appreciated !
Thank you :)
|