16

The Hibernate EntityManager documentation states, that:

You may use a combination of all three together, annotations without JPA programming interfaces and lifecycle, or even pure native Hibernate Core, depending on the business and technical needs of your project. You can at all times fall back to Hibernate native APIs, or if required, even to native JDBC and SQL.

Code that uses the JPA API (EntityManager) is clearly more portable (even with occasional fallbacks to Hibernate Core).

But would I have any advantages when using purely Hibernate Core? I wonder, if the JPA 2 model really fits on top of Hibernate Core without any contradictions? IOW, is a fallback to Core always easy and without problems?

My main concern is this:

Maybe the differences are not only in the API, but also in the underlying semantics?! (e. g. different transaction/versioning/locking semantics that may conflict: Pessimistic locking is mentioned in the Core documentation, but not in the EntityManager documentation - so could I still use pessimistic locking by falling back to Core without causing problems? Things like that...)

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • Actually, pessimistic locking is in JPA 2.0 so it's a bad example but I get your point. – Pascal Thivent Aug 09 '10 at 15:47
  • @Pascal: So it's just missing in the Hibernate docs (it's in http://docs.jboss.org/hibernate/stable/core/reference/en/html/transactions.html, but not in http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/transactions.html) – Chris Lercher Aug 09 '10 at 15:54
  • It is documented in the EntityManager reference guide: [3.12. Locking](http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/objectstate.html#d0e1634). – Pascal Thivent Aug 09 '10 at 18:50

4 Answers4

13

But would I have any advantages when using purely Hibernate Core?

If JPA 2.0 supports what you need, there is in my opinion no advantage at using Hibernate Core directly (and with JPA 2.0, the gap became more thin, making the need to fallback to Core the exception, not the rule, which is a very good thing).

I wonder, if the JPA 2 model really fits on top of Hibernate Core without any contradictions?

It does since JPA 1.0, the Hibernate developers created Hibernate3 with "JPA in mind" and adopted JPA semantics, defaults, etc in Hibernate3. You might want to listen to Gavin in this Tech Talk: Gavin King on Hibernate3 and EJB3:

In this tech talk King discusses how Hibernate3 builds upon and extends EJB3, addressing such topics as:

  • New features of Hibernate3
  • The relationship between Hibernate3 and the EJB3 container in JBoss
  • What differentiates Hibernate3 from the EJB3 specification
  • Hibernate's custom annotations available outside EJB
  • The future of Hibernate

And according to my practical experience, the fact that Hibernate doesn't contradict with EJB 3 is true.

IOW, is a fallback to Core always easy and without problems?

Whether you use Core directly or not, you are using it (the EntityManager is a wrapper around the Session). So, yes, falling back to Core is easy if you really have to (for things that are still not in the spec like Query By Example, for example). And, no, this won't cause any problems (since you actually are using it, or a subset of it, in JPA).

Related questions

Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Though i use JPA 2.0, i have still one thing that i use from hibernate `@BatchSize`. Which works really well and won't cause any trouble as long that i'm sure my queries only returned the results needed. (no post filtering crap). Because i have some heavy entities, i ended up having queries with just test data that took 850ms to load and bring it down to 150ms. Without loading any useless data. – Walfrat Apr 27 '16 at 13:57
3

It has been clear: depending on the business and technical needs of your project

But would I have any advantages when using purely Hibernate Core ?

Do not forget both Hibernate Annotations and Hibernate EntityManager is built on top of Hibernate core. So one more layer. Besides That, it rely on its mapping metadata stored in XML files. Some users prefer To use XML files instead of annotations because it leaves the domain objects completely independent of the DAO implementation you choose. But when using annotations, Hibernate with JPA book is clear

reduce your lines of code for mapping metadata, compared to the native XML files, and you may like the better refactoring capabilities of annotations

JDBC >> Hibernate core >> Hibernate Annotations

...

JDBC >> Hibernate core >> Hibernate EntityManager

And JPA 2 fits better what JPA 1 has not been provided. A lot of new features is now available such as

  • Object oriented query API
  • Wrapper's @Embeddable
  • Collection of @Embeddables @ElementCollection

And so on...

Do not forget JPA EntityManager allows you retrieve its underlying vendor-specific provider by using getDelegate method if you need features where JPA does not provide.

Arthur Ronald
  • 33,349
  • 20
  • 110
  • 136
1

I like using Hibernate Core directly. I also prefer XML mapping files.

I'm not really interested in using a secondary layer, to access Hibernate core functionality. As far as I am concerned portability in the persistence tier, is a complete non-issue.

There are very few real advantages of the JPA API over Hibernate API -- I saw people using JPA named queries (where Criteria would have probably been clearer & better), and the JPA annotations are somewhat better designed.

Other than that, JPA is just a layer adding complexity & potential overhead -- for no benefit. Architecturally in such a situation, the correct decision would be against using it.

Database portability OTOH is very real. I have custom allocators (generators) I use which are completely portable, and far more performant & simpler than the crazy mess which is the mis-designed Hibernate ones.

This approach has been very successful on multiple major commercial, Government & legacy database re-engineering projects. In short -- focus on what's important -- and an API on top of an API, is not it.

Thomas W
  • 13,940
  • 4
  • 58
  • 76
0

The benefits of using the JPA api far outweigh any disadvantages of using pure hibernate. I am not an expert but I am not aware of any situations where it would be beneficial to to drop down to hibernate. (Dropping down to do pure JDBC is a different matter.)

If anyone has any examples to share I would love to see them.

Jay
  • 19,649
  • 38
  • 121
  • 184
  • I fully agree, that the advantages of JPA are most probably greater. But I'd really like to find out, if there are also any (significant) advantages of using Hibernate Core without the EntityManager... – Chris Lercher Aug 09 '10 at 14:03