-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Criteria Problem
PostPosted: Thu Jan 26, 2006 6:23 am 
Newbie

Joined: Thu Jan 26, 2006 6:19 am
Posts: 19
Guys,
Have anyone used the Criteria Object of Hibernate?
I have a Member object which has "careers" and "educations" as collections. For querying Members whose Career.A = x
careerCriteria = criteria.createCriteria("careers");
careerCriteria.add(...);

But I have to query Members just that, either Career.A is "X" or Education.B is "X".

Is it possible to add a criteria like that? What do you suggest?
PS: I use Hibernate3

Best Regards.
Onur.


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria Problem
PostPosted: Thu Jan 26, 2006 7:38 am 
Beginner
Beginner

Joined: Sun Jan 22, 2006 6:56 am
Posts: 29
onur wrote:
Guys,
Have anyone used the Criteria Object of Hibernate?
I have a Member object which has "careers" and "educations" as collections. For querying Members whose Career.A = x
careerCriteria = criteria.createCriteria("careers");
careerCriteria.add(...);

But I have to query Members just that, either Career.A is "X" or Education.B is "X".

Is it possible to add a criteria like that? What do you suggest?
PS: I use Hibernate3

Best Regards.
Onur.


Try this:

membersCriteria = createCriteria(Member.class);
membersCriteria.add(
Restrictions.disjunction()
.add(Subqueries.propertyEq("careers", careersCriteria))
.add(Subqueries.propertyEq("educations", educationsCriteria)))

AFAIK there is no way to perform joins in criteria subqueries, so i'm not sure there's another way (except for going HQL...)


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria Problem
PostPosted: Fri Jan 27, 2006 7:03 am 
Newbie

Joined: Thu Jan 26, 2006 6:19 am
Posts: 19
assaf27 wrote:
Try this:

membersCriteria = createCriteria(Member.class);
membersCriteria.add(
Restrictions.disjunction()
.add(Subqueries.propertyEq("careers", careersCriteria))
.add(Subqueries.propertyEq("educations", educationsCriteria)))

AFAIK there is no way to perform joins in criteria subqueries, so i'm not sure there's another way (except for going HQL...)


The thing is,
using Subqueries.properyEq(String, DetachedCriteria), how will I say
Educations.A = x

in the code quoted, membersCriteria is instance of Criteria, however this method requests DetachedCriteria


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria Problem
PostPosted: Sun Jan 29, 2006 2:58 pm 
Beginner
Beginner

Joined: Sun Jan 22, 2006 6:56 am
Posts: 29
onur wrote:
The thing is,
using Subqueries.properyEq(String, DetachedCriteria), how will I say
Educations.A = x

in the code quoted, membersCriteria is instance of Criteria, however this method requests DetachedCriteria


The subqueries are created as DetachedCriteria. There shouldn't be any difference from a regular query. For example,

Code:
DetachedCriteria educationsCriteria = DetachedCriteria.forClass(Education.class);
educationsCriteria.add(Restrictions.eq("A", "X"));

// same for careersCriteria...

membersCriteria = createCriteria(Member.class);
membersCriteria.add(
    Restrictions.disjunction()
        .add(Subqueries.propertyEq("careers", careersCriteria))
        .add(Subqueries.propertyEq("educations", educationsCriteria)))

membersCriteria.list();


The external query will return only those members that the subquries succeeded for. You may need to use DISTINCT_ROOT_ENTITY though, to avoid duplicate members.

HTH


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