Sponsored Links
Ad by Google
In our previous post we have seen an example of second level cache. Second level cache in hibernate is very popular and you should know about How to configured second level cache in hibernate, here is a complete step by step example of second level cache.
In this post, I am going to show you few examples of Criteria query in hibernate. We all know that Query is one of the core interface in hibernate and here I will show you how to use Criteria query.
The org.hibernate.Criteria is an interface and used to fetch the records from the database based on some conditions. Session is a factory for Criteria and can be obtained from the session, To use Criteria you need criterion instances and criterion instances can be obtained from the org.hibernate.criterion.Restrictions, Restrictions is a class having built in static factory method.
OK, Lets see few examples of Criteria Query, In this post we are using the same project which we were created in our one-to-many association example here.
Let's see all the queries into our AuthorDAO class
AuthorDAO.java
Main.java
OUT PUT:
Download the complete example from here Source Code
In this post, I am going to show you few examples of Criteria query in hibernate. We all know that Query is one of the core interface in hibernate and here I will show you how to use Criteria query.
The org.hibernate.Criteria is an interface and used to fetch the records from the database based on some conditions. Session is a factory for Criteria and can be obtained from the session, To use Criteria you need criterion instances and criterion instances can be obtained from the org.hibernate.criterion.Restrictions, Restrictions is a class having built in static factory method.
OK, Lets see few examples of Criteria Query, In this post we are using the same project which we were created in our one-to-many association example here.
Criteria Query to fetch all the records -
Criteria cr = session.createCriteria(Author.class); return cr.list();The above query will return all the authors from the persistence.
Criteria Query using like % operator in query -
Criteria cr = session.createCriteria(Author.class); cr.add(Restrictions.like("firstName", "J%")); return cr.list();The above query will return all the author whose name start with J characters.
Criteria Query example using between operator -
Criteria cr = session.createCriteria(Author.class); cr.add(Restrictions.between("dateOfBirth", dob, dob2)); return cr.list();The above query will return all the authors whose date of birth is between provided dates.
Criteria Query to fetch total row count of records -
Criteria cr = session.createCriteria(Author.class); cr.setProjection(Projections.rowCount()); return cr.list();The above query will return number of record count in the database.
Criteria Query Example using And Operator -
Criteria cr = session.createCriteria(Author.class); cr.add(Restrictions.between("dateOfBirth", dob, dob2)).add(Restrictions.and(Restrictions.like("firstName", "Kh%"))); return cr.list();The above query will return all records which will full fill both the conditions.
Criteria Query example using order by clause -
Criteria cr = session.createCriteria(Author.class); cr.addOrder(Order.asc(property)); return cr.list();The above query will return all the records in ascending order of given property.
Criteria Query example using pagination
Criteria cr = session.createCriteria(Author.class); cr.setFirstResult(20); cr.setMaxResults(10); return cr.list();The above query will return 10 records start from 20th row of records.
Criteria Query example using limit
Criteria cr = session.createCriteria(Author.class); cr.setMaxResults(30); return cr.list();The above query will return max 30 records from the persistence.
Let's see all the queries into our AuthorDAO class
AuthorDAO.java
package com.javamakeuse.poc.dao; import java.util.Date; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import com.javamakeuse.poc.pojo.Author; import com.javamakeuse.poc.util.HibernateUtility; public class AuthorDAO { private static SessionFactory sessionFactory; static { sessionFactory = HibernateUtility.getSessionFactory(); } // criteria query example to get all authors public static List<Author> getAllAuthors() { Session session = sessionFactory.openSession(); Criteria cr = session.createCriteria(Author.class); return cr.list(); } // criteria with limit query public static List<Author> getAllAuthors(int limit) { Session session = sessionFactory.openSession(); Criteria cr = session.createCriteria(Author.class); cr.setMaxResults(10); return cr.list(); } // criteria query with asc/desc order public static List<Author> getAllAuthors(int limit, String property) { Session session = sessionFactory.openSession(); Criteria cr = session.createCriteria(Author.class); cr.addOrder(Order.asc(property)); cr.setMaxResults(limit); return cr.list(); } // criteria query with like operator public static List<Author> getAllAuthors(String nameStartWith) { Session session = sessionFactory.openSession(); Criteria cr = session.createCriteria(Author.class); cr.add(Restrictions.like("firstName", "J%")); return cr.list(); } // criteria query with between operator public static List<Author> getAllAuthors(Date dob, Date dob2) { Session session = sessionFactory.openSession(); Criteria cr = session.createCriteria(Author.class); cr.add(Restrictions.between("dateOfBirth", dob, dob2)); return cr.list(); } // criteria query using rowCount method public static List<Author> rowCount() { Session session = sessionFactory.openSession(); Criteria cr = session.createCriteria(Author.class); cr.setProjection(Projections.rowCount()); return cr.list(); } // criteria query using and operator public static List<Author> andOperator(Date dob, Date dob2) { Session session = sessionFactory.openSession(); Criteria cr = session.createCriteria(Author.class); cr.add(Restrictions.between("dateOfBirth", dob, dob2)).add( Restrictions.and(Restrictions.like("firstName", "Kh%"))); return cr.list(); } // criteria query using pagination public static List<Author> paginationExample() { Session session = sessionFactory.openSession(); Criteria cr = session.createCriteria(Author.class); cr.setFirstResult(20); cr.setMaxResults(10); return cr.list(); } }
Main.java
package com.javamakeuse.poc.service; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import com.javamakeuse.poc.dao.AuthorDAO; public class Main { public static void main(String[] args) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date year1 = sdf.parse("1761-04-20"); Date year2 = sdf.parse("1961-04-20"); System.out.println(AuthorDAO.paginationExample().size()); System.out.println(AuthorDAO.getAllAuthors(year1, year2)); System.out.println(AuthorDAO.getAllAuthors(5, "firstName")); } catch (ParseException e) { e.printStackTrace(); } } }
OUT PUT:
Hibernate: select this_.author_id as author_i1_0_0_, this_.dob as dob2_0_0_, this_.first_name as first_na3_0_0_, this_.last_name as last_nam4_0_0_ from author this_ where this_.dob between ? and ? Hibernate: select books0_.author_id as author_i4_0_1_, books0_.book_id as book_id1_1_1_, books0_.book_id as book_id1_1_0_, books0_.author_id as author_i4_1_0_, books0_.isbn as isbn2_1_0_, books0_.title as title3_1_0_ from book books0_ where books0_.author_id=? [Author [authorId=2, firstName=Khalid, lastName=Mughal, dateOfBirth=1861-04-20, books=1]] Hibernate: select this_.author_id as author_i1_0_0_, this_.dob as dob2_0_0_, this_.first_name as first_na3_0_0_, this_.last_name as last_nam4_0_0_ from author this_ order by this_.first_name asc limit ? Hibernate: select books0_.author_id as author_i4_0_1_, books0_.book_id as book_id1_1_1_, books0_.book_id as book_id1_1_0_, books0_.author_id as author_i4_1_0_, books0_.isbn as isbn2_1_0_, books0_.title as title3_1_0_ from book books0_ where books0_.author_id=? Hibernate: select books0_.author_id as author_i4_0_1_, books0_.book_id as book_id1_1_1_, books0_.book_id as book_id1_1_0_, books0_.author_id as author_i4_1_0_, books0_.isbn as isbn2_1_0_, books0_.title as title3_1_0_ from book books0_ where books0_.author_id=? Hibernate: select books0_.author_id as author_i4_0_1_, books0_.book_id as book_id1_1_1_, books0_.book_id as book_id1_1_0_, books0_.author_id as author_i4_1_0_, books0_.isbn as isbn2_1_0_, books0_.title as title3_1_0_ from book books0_ where books0_.author_id=? Hibernate: select books0_.author_id as author_i4_0_1_, books0_.book_id as book_id1_1_1_, books0_.book_id as book_id1_1_0_, books0_.author_id as author_i4_1_0_, books0_.isbn as isbn2_1_0_, books0_.title as title3_1_0_ from book books0_ where books0_.author_id=? Hibernate: select books0_.author_id as author_i4_0_1_, books0_.book_id as book_id1_1_1_, books0_.book_id as book_id1_1_0_, books0_.author_id as author_i4_1_0_, books0_.isbn as isbn2_1_0_, books0_.title as title3_1_0_ from book books0_ where books0_.author_id=? [Author [authorId=1, firstName=Joshua, lastName=Bloch, dateOfBirth=1961-08-28, books=1], Author [authorId=2, firstName=Khalid, lastName=Mughal, dateOfBirth=1861-04-20, books=1], Author [authorId=4, firstName=Mukesh, lastName=null, dateOfBirth=null, books=1], Author [authorId=5, firstName=Pan, lastName=null, dateOfBirth=null, books=1], Author [authorId=3, firstName=Tony, lastName=Tony, dateOfBirth=1962-07-28, books=1]]That's it,
Download the complete example from here Source Code
Sponsored Links
0 comments:
Post a Comment