-->
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: How to write an HQL for ternary association ?
PostPosted: Tue Aug 26, 2003 11:41 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
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 :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 12:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you want to perform queries directly against the join table, I suggest you map it as an entity.


Top
 Profile  
 
 Post subject: How do I do that ?
PostPosted: Wed Aug 27, 2003 1:27 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 11:33 pm
Posts: 38
Map as an entity ? Could you give me some example ? I'm a newbie.


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.