Criteria API Examples In Hibernate
By AmarSivas | | Updated : 2021-01-20 | Viewed : 47 times

Table of Contents:
Hibernate Criteria Api
Criteria And Criterion Difference
Criteria criteriaObject = session.createCriteria(entity.class);
Criterion criterionObject = Restrictions.like("playerType","Bats%", MatchMode.ANYWHERE);
criteriaObject.add(criterionObject );
In the first line, we did create the
Criteria Api Examples
To fetch the entity list using criteria we can use the below-given code
Fetch Entity List Using Criteria
Criteria crit = session.createCriteria(Cricketer.class);
List<Cricketer> results = crit.list();
Criterion In Hibernate
Restrictions In Hibernate Criteria
criteria1.add(Restrictions.eq("playerType","Bowler"));
The above-given Criterion will be working as
criteria2.add(Restrictions.ne("salaryIncome",450000l));
Here query will be prepared as with
criteria4.add(Restrictions.isNull("jerseyNo"));
criteria5.add(Restrictions.isNotNull("jerseyNo"));
Here these two code snippets work as SQL operators
criteria6.add(Restrictions.gt("salaryIncome", 450000l));
criteria7.add(Restrictions.lt("salaryIncome", 500000l));
Here in example4, these two code snippets work as SQL parameters
To find more examples for
Projection In Hibernate Criteria
To create Criteria object for the
ProjectionList projList = Projections.projectionList();
projList.add(Projections.max("age"));
projList.add(Projections.min("age"));
projList.add(Projections.avg("age"));
projList.add(Projections.countDistinct("playerType"));
criteria18.setProjection(projList);
To find more examples please have a look at Github repository Hibernate-Criteria-Examples-App