-->
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.  [ 1 post ] 
Author Message
 Post subject: How to combine two detachedCriteria with logical operators
PostPosted: Fri Apr 02, 2010 11:07 pm 
Newbie

Joined: Tue Aug 29, 2006 2:54 am
Posts: 16
Dear all , I have two available two detachedCriteria as below
Code:
public Class CriteriaFactory
{

public static DetachedCriteria getAgeAbove20Cri()
{
DetachedCriteria ageAbove20Cri = DetachedCriteria.forClass( Student.class )
        .add(Restrictions.gt("age" , 20 ) );
  return ageAbove20Cri ;

}

public static DetachedCriteria getAgeBelow10Cri()
{
DetachedCriteria ageBelow10Cri = DetachedCriteria.forClass( Student.class )
        .add(Restrictions.lt("age" , 10 ) );
return ageBelow10Cri;
}
}


Now I want to reuse these two detachedCriteria to get the student that age above 20 or age below 10.

My current way is
Code:
DetachedCriteria ageAbove20Cri = CriteriaFactory.getAgeAbove20Cri()
DetachedCriteria ageBelow10Cri = CriteriaFactory.getAgeBelow10Cri()
List ageAbove20StudentList = ageAbove20Cri.getExecutableCriteria( session ).list();
List ageBelow10StudentList = ageBelow10Cri .getExecutableCriteria( session ).list();

List studentList = new ArrayList();
studentList.addAll(ageAbove20StudentList );
studentList.addAll(ageBelow10StudentList );


The code will use two separate sql to get the student data
( 1.get student age above 20 , 2. get student age below 10).

I want to let hibernate use one sql to get the data
(student age above 20 or student age below 10).

But the code can't work, because the parameter type of Restrictions.or is Criterion.
Code:
this.session.createCriteria( Student.class )
.add( Restrictions.or( ageAbove20Cri, ageBelow10Cri  ) ).list();


I want to reuse the available detachedCriteria.
Can somebody teach me how to combine two availabe detachedCriteria with logical operators?

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.