This method tells Hibernate to retrieve a fixed number maxResults of objects. Public Criteria setMaxResults(int maxResults) This method takes an integer that represents the first row in your result set, starting with row 0. Public Criteria setFirstResult(int firstResult) There are two methods of the Criteria interface for pagination. Though all the above conditions can be used directly with HQL as explained in previous tutorial. LogicalExpression andExp = Restrictions.and(salary, name) To get records matching with AND conditions LogicalExpression orExp = Restrictions.or(salary, name) To get records matching with OR conditions You can create AND or OR conditions using LogicalExpression restrictions as follows −Ĭriterion salary = Restrictions.gt("salary", 2000) Ĭriterion name = Restrictions.ilike("firstNname","zara%") To check if the given property is not emptyĬr.add(Restrictions.isNotEmpty("salary")) To check if the given property is empty To check if the given property is not nullĬr.add(Restrictions.isNotNull("salary")) To check if the given property is null To get records having salary in between 10Ĭr.add(Restrictions.between("salary", 1000, 2000)) ![]() Case sensitive form of the above restriction.Ĭr.add(Restrictions.ilike("firstName", "zara%")) To get records having fistName starting with zaraĬr.add(Restrictions.like("firstName", "zara%")) To get records having salary less than 2000 To get records having salary more than 2000 Following is the example to add a restriction to return the records with salary is equal to 2000 −įollowing are the few more examples covering different scenarios and can be used as per the requirement − ![]() You can use add() method available for Criteria object to add restriction for a criteria query. The Hibernate Session interface provides createCriteria() method, which can be used to create a Criteria object that returns instances of the persistence object's class when your application executes a criteria query.įollowing is the simplest example of a criteria query is one, which will simply return every object that corresponds to the Employee class.Ĭriteria cr = session.createCriteria(Employee.class) One of the methods is Criteria API, which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions. Hibernate provides alternate ways of manipulating objects and in turn data available in RDBMS tables.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |