In the last post we saw restrictions associated with equality and comparison.We can even use a single criterion instance to apply multiple equality conditions.
public static void testAllEq() { Map<String, Object> eqValuesMap = new HashMap<String, Object>(); eqValuesMap.put("name", "entity2"); eqValuesMap.put("id", 3); final Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Entity.class); criteria.add(Restrictions.allEq(eqValuesMap)); Entity entity = (Entity) criteria.uniqueResult(); System.out.println(entity); }The result is :
Hibernate:
/* criteria query */
select
this_.ID as ID0_0_,
this_.NAME as NAME0_0_,
this_.DATE as DATE0_0_,
this_.MASTER_ID as MASTER4_0_0_
from
ENTITY this_
where
(
this_.ID=?
and this_.NAME=?
)
[Entity] : ( id 3 , data : entity2 , master.Id : 1 , date : null )]
If we only need to fetch an object on the basis of id:
public static void testIdEq() { final Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Entity.class); criteria.add(Restrictions.idEq(2)); Entity entity = (Entity) criteria.uniqueResult(); System.out.println(entity); }The resultant query is :
Hibernate: /* criteria query */ select this_.ID as ID0_0_, this_.NAME as NAME0_0_, this_.DATE as DATE0_0_, this_.MASTER_ID as MASTER4_0_0_ from ENTITY this_ where this_.ID = ? [Entity] : ( id 2 , data : entity1 , master.Id : 1 , date : null )]To apply a between condition in criteria we have the BetweenExpression:
public static void testBetween() { final Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Entity.class); criteria.add(Restrictions.between("id", 2, 5)); List<Entity> entities = criteria.list(); System.out.println(entities); }The sql generated is:
select this_.ID as ID0_0_, this_.NAME as NAME0_0_, this_.DATE as DATE0_0_, this_.MASTER_ID as MASTER4_0_0_ from ENTITY this_ where this_.ID between ? and ?Similarly to apply an in condition we have the InExpression.
public static void testIn() { final Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Entity.class); criteria.add(Restrictions.in("id", new Integer[]{2,3,4})); List<Entity> entities = criteria.list(); System.out.println(entities); }The sql generated is :
select this_.ID as ID0_0_, this_.NAME as NAME0_0_, this_.DATE as DATE0_0_, this_.MASTER_ID as MASTER4_0_0_ from ENTITY this_ where this_.ID in ( ?, ?, ? )The method can work with an array as well as a collection. For collections we have the EmptyExpression criterion.
public static void testEmptyCollection() { final Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Entity.class); criteria.add(Restrictions.isEmpty("children")); List<Entity> entities = criteria.list(); System.out.println(entities); }The resultant output is:
Hibernate: /* criteria query */ select this_.ID as ID0_0_, this_.NAME as NAME0_0_, this_.DATE as DATE0_0_, this_.MASTER_ID as MASTER4_0_0_ from ENTITY this_ where not exists ( select 1 from CHILD_ENTITY where this_.ID=ENTITY_ID ) []Similarly for a not empty condition we have NotEmptyExpression. To check the collection size we have the SizeExpression.
public static void testCollectionSize() { final Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(Entity.class); criteria.add(Restrictions.sizeGe("children", 2)); List<Entity> entities = criteria.list(); System.out.println(entities); }The result is:
Hibernate: /* criteria query */ select this_.ID as ID0_0_, this_.NAME as NAME0_0_, this_.DATE as DATE0_0_, this_.MASTER_ID as MASTER4_0_0_ from ENTITY this_ where ? <= ( select count(*) from CHILD_ENTITY where this_.ID=ENTITY_ID ) [[Entity] : ( id 2 , data : entity1 , master.Id : 1 , date : null )]]The size can also be tested against not equal/less than/less than or equal/greater than and greater than or equal.
I will share every one of your good posts with my friends and also I wish they will certainly appreciate to read your guides.
ReplyDeleteWhat is Zakat and How to calculate Zakat?
What is Umrah and How to Perform Umrah?
What is Ramadan
What is Hajj and Why is Hajj important in islam?
Five Pillars of Islam
Url opener