Search code examples
hibernatejpacriteria

Is legacy Criteria API still recommended?


My team has been using hibernate Criteria API for a long time under Hibernate 3.x. Recently, we want to upgrade Hibernate version to 5, but the Criteria API seemed not be recommended any more. But JPA looks like a totally different API which we don't want to risk. Should we keep using the old Criteria?


Solution

  • Well, the warning in the Version 5.0 Criteria Chapter is pretty darn big:

    Hibernate offers an older, legacy org.hibernate.Criteria API which should be considered deprecated. No feature development will target those APIs. Eventually, Hibernate-specific criteria features will be ported as extensions to the JPA javax.persistence.criteria.CriteriaQuery. For details on the org.hibernate.Criteria API, see Legacy Hibernate Criteria Queries.

    And there's more:

    Users of the older Hibernate org.hibernate.Criteria query API will recognize the general approach, though we believe the JPA API to be superior as it represents a clean look at the lessons learned from that API.

    So, it seems like it's really up to your team. It took me a while to get used to the JPA API, but it's pretty smooth sailing now. I have dropped back into Hibernate for performance for very large fetches, but in general I'm not having any problem using anything JPA. I tend to use @NamedQueries for basic acccess, and the Criteria API if I need something more dynamic.

    EDIT: Update I was able to resolve the performance issue and the application is strictly JPA now.