aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate - Object Graph using outer join Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate - Object Graph using outer join" Watch "Hibernate - Object Graph using outer join" New topic
Author

Hibernate - Object Graph using outer join

Bhavin Sanghani
Ranch Hand

Joined: Dec 17, 2003
Posts: 67
Hi,

I want to create Object Graph from three (or more) related tables by querying it with criteria. I have learnt that using left join fetch we can do it in single query but it seems there are certain limitations.


I have following mappings:

Question.hbm.xml
<class name="Question" table="QUESTION">
<id name="questionId" type="java.lang.Long" column="QUESTIONID" >
<generator class="increment" />
</id>
<property name="questionName" type="java.lang.String" column="QUESTIONNAME" length="500" />
<many-to-one name="questionType" column="QUESTIONTYPEID" class="QuestionType"/>
<many-to-one name="questionSet" column="QUESTIONSETID" class="QuestionSet" not-null="true" fetch="join"/>
<one-to-one name="comment" class="Comment" property-ref="question" cascade="all"/>
<one-to-one name="matrix" class="Matrix" property-ref="question" cascade="all"/>
<bag name="answers" inverse="true" cascade="all, delete-orphan" lazy="false">
<key column="questionId"/>
<one-to-many class="Answer"/>
</bag>
<bag name="matrix" inverse="true" cascade="all, delete-orphan" lazy="false">
<key column="questionId"/>
<one-to-many class="Matrix"/>
</bag>
<bag name="features" inverse="true" cascade="all" lazy="false">
<key column="questionId"/>
<one-to-many class="Feature"/>
</bag>
<bag name="rules" cascade="all" lazy="false">
<key column="questionId"/>
<one-to-many class="Rule"/>
</bag>
</class>


Answer.hbm.xml:

<class name="Answer" table="ANSWER">
<id name="answerId" type="java.lang.Long" column="ANSWERID" >
<generator class="increment" />
</id>
<property name="answerName" type="java.lang.String" column="ANSWERNAME" length="50" />
<many-to-one name="question" column="questionId" class="Question" not-null="true" fetch="join"/>
<bag name="features" cascade="all" lazy="false">
<key column="ANSWERID"/>
<one-to-many class="Feature"/>
</bag>
</class>



Similar way Features.hbm.xml and Rules.hbm.xml. Now, I want to pass questionSetId (parent) and bring all the records in the collection and do the processing...

1) Is this preferred approach?

2) If yes, then it fires lots of queries. Should I fire explicit queries and create Object Graph manually? How can I avoid lots of queries and create Object Graph in one (or minimal) query.

3) If no, can you advice me for preferred approach in this example?

I am using Table per Concrete Class approach.


Thanks,
Bhavin




Thanks,
Bhavin
[ December 09, 2008: Message edited by: Bhavin Sanghani ]
Bhavin Sanghani
Ranch Hand

Joined: Dec 17, 2003
Posts: 67
Can anyone advice me on this?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate - Object Graph using outer join