I am upgrading to latest Hibernate 5.2.0 FINAL from Hibernate 3.x. In my old code we were using criteria queries as below.
Session session =getHibernateTemplate().getSessionFactory().getCurrentSession();
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.eq("Department", department));
return criteria.list();
Now from Hibernate 5.2.0 the createCriteria() method has been deprecated. Which can be found from the following documentation.
https://docs.jboss.org/hibernate/orm/5.2/javadocs/deprecated-list.html https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/SharedSessionContract.html#createCriteria-java.lang.Class-
The documentation suggests to use JPA Criteria. Below are the few questions I have based on the above background.
Since we are not using the EntityManager and heavily dependent on the HibernateDAOSupport and HibernateTemplate , how I can use the JAP Criteria using the session or sessionFactory ?
If I use DetachedCriteria as in the below code snippet is it going to be the same as previous implementation or the below code will give us session independent results ?
DetachedCriteria criteria = DetachedCriteria.forClass(Employee.class); criteria.add(Restrictions.eq("Department", department)); return (List<Employee>) getHibernateTemplate().findByCriteria(criteria);
Also as an alternative , If I use the DetachedCriteria in below mentioned way is it going to have the same impact as of my old code.
Session session =getHibernateTemplate().getSessionFactory().getCurrentSession(); DetachedCriteria criteria = DetachedCriteria.forClass(Employee.class); criteria.add(Restrictions.eq("Department", department)); return criteria .getExecutableCriteria(session).list();
If there is a better way to deal with this please suggest as I don't want to change the use of HibernateDAOSupport and HibernateTemplate.