3

Recently my team upgraded from Hibernate 3 to Hibernate 5 so I am working on the migration stuff. Now when I try to use CriteriaQuery class, eclipse shows that it can be imported from both 'javax.persistence.criteria' as well as 'org.hibernate.criterion' packages.

CriteriaQuery cq = getSession().getCriteriaBuilder().createQuery(MyClass.class);

So I am a bit confused over here about which one to use.

Also it would be very helpful if someone could explain the difference between the two(JPA and Hibernate) as I am unclear about this conceptually and have a hard time getting to know whats happening underneath.

DockYard
  • 989
  • 2
  • 12
  • 29

1 Answers1

4

From Hibernate docs:

This appendix covers the legacy Hibernate org.hibernate.Criteria API, which should be considered deprecated.

You should use the JPA criteria query, which was heavily influenced by Hibernate. At the beginning, there were quite a bit of differences (for example, LAZY vs EAGER fetch as defaults on mappings.) by the time Hibernate 5 came around, most of the differences have been resolved by Hibernate adopting JPA behavior. As far as I understand most Hibernate behavior is now either the same or similar to the JPA standards, and is only maintained for legacy purposes.

Jeff Wang
  • 1,837
  • 1
  • 15
  • 29
  • So is the javax.persistence.* packages built over hibernate? – DockYard Jun 06 '18 at 04:58
  • 1
    I wouldn't say "built over". There was a lot of copied code, but during the initial implementation, there were changes in behavior. Each ORM implements the behavior, and if it passes the test suite, it is judged to be JPA compliant. Hibernate initially took similar code, and in some cases, just slapped on an extra annotation to scan as their behavior was the same. Over time, it is preferable to use the JPA annotations, as they are standard. There's very little reason to use the hibernate API at this point. – Jeff Wang Jun 07 '18 at 00:37