-->
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.  [ 10 posts ] 
Author Message
 Post subject: one to many relationship with more than one condition
PostPosted: Mon Feb 28, 2011 3:28 am 
Newbie

Joined: Mon Feb 28, 2011 2:17 am
Posts: 5
I have two tables as mentioned below.

<class table="QUESTION" name="Question">
<id column="QUESTION_ID" length="10" name="questionId" type="integer">
<generator class="sequence">
<param name="sequence">QUESTION_ID_SEQ</param>
</generator>
</id>
<property column="IS_OBJECTIVE_TYPE" length="1" name="isObjectiveType" not-null="false" type="boolean" />
<property column="QUESTION_DESC" length="500" name="questionDesc" not-null="false" type="string" />
<set cascade="none" lazy="true" name="answersSet" order-by="ANSWER_ID asc">
<key>
<column name="QUESTION_ID" />
</key>
<one-to-many class="Answers" />
</set>
</class>

<class table="ANSWER" name="Answers">
<id column="ANSWER_ID" length="10" name="answerId" type="integer">
<generator class="sequence">
<param name="sequence">ANSWER_ID_SEQ</param>
</generator>
</id>
<property column="IS_OBJECTIVE_TYPE" length="1" name="isObjectiveType" not-null="false" type="boolean" />
<property column="ANSWER_DESC" length="500" name="answerDesc" not-null="false" type="string" />
</class>

I have more than one answer for the given question. here i want to fetch the questions along with the answers those are objective type.

Session session = this.getSessionFactory().getCurrentSession();
Criteria objCriteria = session.createCriteria(Question.class);
objCriteria.add(Restrictions.eq("isObjectiveType", Boolean.TRUE));
List<Question> questionList = (List<Question>) objCriteria.list();

Above list gives objective type question with all answers irrespective of "isObjectiveType" in ANSWER.
Here i want to filter the answers based on "isObjectiveType" in Question entity.

How to filter child records based on additional column other than foreign key reference.
Please help me on this


Top
 Profile  
 
 Post subject: Re: one to many relationship with more than one condition
PostPosted: Mon Feb 28, 2011 3:48 am 
Regular
Regular

Joined: Fri Nov 12, 2010 4:13 am
Posts: 81
Location: India
What is the datatype of isObjectiveType??

_________________
Thanks & Regards,
Chirag


Top
 Profile  
 
 Post subject: Re: one to many relationship with more than one condition
PostPosted: Mon Feb 28, 2011 3:51 am 
Newbie

Joined: Mon Feb 28, 2011 2:17 am
Posts: 5
its Boolean. Based on that i want the child records.


Top
 Profile  
 
 Post subject: Re: one to many relationship with more than one condition
PostPosted: Mon Feb 28, 2011 4:24 am 
Regular
Regular

Joined: Fri Nov 12, 2010 4:13 am
Posts: 81
Location: India
You are not tracing your queries generated by hibernate?? If yes can you paste the query which is generated so that it is easier to know whats the problem is.

_________________
Thanks & Regards,
Chirag


Top
 Profile  
 
 Post subject: Re: one to many relationship with more than one condition
PostPosted: Mon Feb 28, 2011 6:51 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
You put your Criteria Restriction directly on the Question class so your result is normal and expected!
Take a look at this, it will help you understand how to use Criteria API in your case:

http://docs.jboss.org/hibernate/core/3. ... sociations


Top
 Profile  
 
 Post subject: Re: one to many relationship with more than one condition
PostPosted: Mon Feb 28, 2011 8:21 am 
Newbie

Joined: Mon Feb 28, 2011 2:17 am
Posts: 5
Pls find the queries generated by hibernate
Hibernate: select this_.QUESTIONS_ID as QUESTIONS1_21_0_, this_.IS_OBJECTIVE_TYPE as IS2_21_0_, this_.QUESTION_DESC as QUESTION3_21_0_ from QUESTIONS this_ where this_.IS_OBJECTIVE_TYPE=?

overmeulen,
Parent table has nearly '6' one to many child relationship. they were getting the childs on need basis. Application had been built already and i am trying to customize the HBM file to do this. Will you please suggest any idea to solve my problem


Top
 Profile  
 
 Post subject: Re: one to many relationship with more than one condition
PostPosted: Mon Feb 28, 2011 8:57 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Can you tell us exactly what is the SQL query you are trying to achieve? Which data you want to extract and with which conditions? It is not very clear right now given that you have the same field "IS_OBJECTIVE_TYPE" in both your objects.
By the way, which version of Hibernate are you using?


Top
 Profile  
 
 Post subject: Re: one to many relationship with more than one condition
PostPosted: Mon Feb 28, 2011 9:43 am 
Newbie

Joined: Mon Feb 28, 2011 2:17 am
Posts: 5
I am using Hibernate 3.2.

Actually i have two tables "Question" and "Answer". It has one to many set relationship based question_id (foreign key for "Answer" entity). As of now i can get questions along with their no of answers.
When i fetch the records from "Question" basedon on "Objective_Type" it gives the filtered records from "Question" entity with unfiltered(all irrespective of "Objective_Type" in Answer) records from "Answer" entity. So i wanted to have one more condition in set relationship for "Objective_Type".
Here i cant able to create criteria with filter expression to get the records since the application has been built already. they are getting the childs from the parent entity instead of seperate criteria/hql query.

Is it possible to use filter in set relation to filter the child records? if so what will be the scope of filter.


Top
 Profile  
 
 Post subject: Re: one to many relationship with more than one condition
PostPosted: Tue Mar 01, 2011 12:41 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

Right now your query looks like this:

from Question as q where q.isObjectiveType = TRUE

If I understood what you want to achieve, you should create a query like this one:

from Question as q left join q.answersSet answers with answers.isObjectiveType = TRUE where q.isObjectiveType = TRUE


Top
 Profile  
 
 Post subject: Re: one to many relationship with more than one condition
PostPosted: Thu Mar 03, 2011 2:43 am 
Newbie

Joined: Mon Feb 28, 2011 2:17 am
Posts: 5
Yes. Correct i want to get details based on "isObjectiveType" from "Question" and "Answer" entity(like Questions.isObjectiveType = Answers.isObjectiveType).
Here the problem is the application already gone to production environment and they asked us to add "isObjectiveType" field and this question entity is used in many places accross application. Is it possible to achieve the same using configuring hbm files by defining set relation conditions or some filters.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.