I need to execute a query to fetch all entities with id is greater than 5 and the names start with "ent". That means there are two distinct conditions. Doing that using HQL would be pretty straightforward:
But what if need an OR condition ?
What if we need to add an additional and clause to the above or clauses. That is I need all entities whose id is greater than 5 or the names start with "ent" . But the master for all records must have id equal to 1.
publicstatic void testTwoConditions() {Doing the same in Criteria means applying two criterions on the same criteria instance.
final Session session = sessionFactory.openSession();
Query q = session.createQuery("from Entity e where e.id > 3 " +
"and e.name like 'ent%'");
List<Entity> entities = q.list();
System.out.println(entities);
}
publicstatic void testTwoConditions() {The query is :
final Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(Restrictions.gt("id",3));
criteria.add(Restrictions.like("name", "ent",MatchMode.START));
List<Entity> entities = criteria.list();
System.out.println(entities);
}
selectAs can be seen applying two conditions together is as simple as adding two criterions. Every addition of a Criterion instance will result in an additional and condition being applied to the where clause of the SQL query.
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 like ?
But what if need an OR condition ?
publicstatic void testTwoOrConditions() {The query will be:
final Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(Restrictions.or(Restrictions.gt("id",3),
Restrictions.like("name", "ent",MatchMode.START)));
List<Entity> entities = criteria.list();
System.out.println(entities);
}
selectThe other way to do the same would be using the Restrictions.disjunction() method:
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>?
or this_.NAME like ?
)
publicstatic void testDisjunction() {This will also generate an identical query.
final Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(
Restrictions.disjunction()
.add(Restrictions.gt("id",3))
.add(Restrictions.like("name", "ent",MatchMode.START)
)
);
List<Entity> entities = criteria.list();
System.out.println(entities);
}
What if we need to add an additional and clause to the above or clauses. That is I need all entities whose id is greater than 5 or the names start with "ent" . But the master for all records must have id equal to 1.
publicstatic void testDisjunctionWithConjuction() {The query generated is:
final Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(
Restrictions.disjunction()
.add(Restrictions.gt("id", 3))
.add(Restrictions.like("name", "ent", MatchMode.START))
)
.add(Restrictions.eq("master.id", 1l)
);
List<Entity> entities = criteria.list();
System.out.println(entities);
}
selectThe same result will be produced with the below method:
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>?
or this_.NAME like ?
)
and this_.MASTER_ID=?
publicstatic void testOrFollowedByAnd() {I decided to tweak the query a little - I need all entities whose id is greater than 5 and the names start with "ent" . Or the master must have id equal to 1.The Criteria query now is :
final Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(
Restrictions.or(
Restrictions.gt("id",3),
Restrictions.like("name", "ent",MatchMode.START)
))
.add(
Restrictions.eq("master.id",1l)
);
List<Entity> entities = criteria.list();
System.out.println(entities);
}
publicstatic void testAndFollowedByOr() {The query generated is :
final Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Entity.class);
criteria.add(
Restrictions.or(
Restrictions.and(
Restrictions.gt("id",3),
Restrictions.like("name", "ent",MatchMode.START)
)
,Restrictions.eq("master.id",1l)
)
);
List<Entity> entities = criteria.list();
System.out.println(entities);
}
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 like ?
)
or this_.MASTER_ID=?
)
[[Entity] : ( id 2 , data : entity1 , master.Id : 1 , date : null )], [Entity] :
( id 3 , data : entity2 , master.Id : 1 , date : null )], [Entity] : ( id 4 , d
ata : entity100 , master.Id : 2 , date : null )], [Entity] : ( id 5 , data : ent
ity102 , master.Id : 2 , date : null )]]