This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hibernate - Object Graph using outer join  RSS feed

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone advice me on this?
 
Paper beats rock. Scissors beats tiny ad.
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!