18

I have been moving around with this question and the bird eye difference between Hibernate SessionFactory and JPA EntityManagerFactory is that JPA is standard. You can use it without being afraid of underlying ORM. yet it calls the underlying sessionFactory under the hood.(Correct me if i am wrong)

But if someone knows that he has just to stick with hibernate as a ORM in the future, then what should he choose from these two Factories and why?

Secondly, what are the other differences between these two with respect to performance, features, stability etc?

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
vicky
  • 561
  • 1
  • 7
  • 17
  • Presumably the downvoter voted based on the fact that this question has been asked many times before on here, and simple search would tell you what you need to know (does it "take guts" to use search?). But then you'd have to ask them why ... – Neil Stockton May 04 '14 at 14:24
  • 4
    So if i tell you that i did all search and i wanted to know about some specific aspects which were not asked in previous ones like performance n features of two. If u think the same question has been asked in exactly the same manner many times, mark and tell us the duplicate. But down voted means question that is not worth to post on SO, i believe. I know the worth and searched a lot about this on web. – vicky May 04 '14 at 15:01

1 Answers1

48

You should prefer the standard JPA API over the proprietary Hibernate one, for several reasons:

  1. It makes you learn something that you can reuse in more other projects, relying on a different implementation
  2. The JPA API is cleaner than the Hibernate one: it doesn't have the early mistakes that the Hibernate API has
  3. The efforts and evolutions are now targeted at the JPA API. For example, the standard JPA2 criteria API is more complete than the old, proprietary, Hibernate Criteria API (but more complex to use, IMHO)
  4. If you want, you can always get the Hibernate Session from the JPA EntityManager. Not vice-versa

Anyway, most of the effort is in mapping the entities themselves, and that is done using standard JPA annotations, even when using the Session API.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • pardon me for my less knowledge about JPA big bro. What will be the performance or efficiency effect when one fetch session from JPA factory rather than direct from Hibernate factory? – vicky May 03 '14 at 16:22
  • 2
    There won't be any noticeable difference. This should never happen anyway. What takes most of the time in an application using databases is to execute queries. – JB Nizet May 03 '14 at 16:28
  • Thanks for your precious time and teaching me the stuff :-) – vicky May 03 '14 at 16:49
  • 1
    @JBNizet When using `EntityManager` instead of Hibernate `Session`, how can we achieve Transaction Demarcation using Plain JDBC Thread-bound strategy as shown [here](https://developer.jboss.org/wiki/SessionsAndTransactions#jive_content_id_Transaction_demarcation_with_plain_JDBC)? – Akshat May 30 '15 at 14:08
  • 1
    @JBNizet I am creating a project with JavaSE so Demarcation using EJB/CMT (managed environment) is not possible! And, in non-managed environment using [this type of demarcation](https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/transactions.html#transactions-demarcation-nonmanaged) will not be as cleaner as [this](https://developer.jboss.org/wiki/SessionsAndTransactions#jive_content_id_Transaction_demarcation_with_plain_JDBC), because we do not need to close `Session` like we have to do with `EntityManager`! – Akshat May 30 '15 at 14:13