EJB 3 or Hibernate 3
Asked Answered
C

3

12

Regarding a Java EE Web application which is going to be served by a full Java EE Application server e.g. GlassFish, which is the best ORM Solution? EJB 3 or Hibernate 3 And why?

Custodial answered 9/1, 2011 at 13:58 Comment(1)
There are plenty of other persistence solutions that have distinct advantages; you seem to have ignored them in your question, and so any answer of what is the "best" is arbitrary. Likely what is the best is dependent on the application itself, the developers who are writing it, and the datastore being used ... and you haven't provided information on thoseBalky
C
30

Those two are completely different.

EJB3 is a component model and has itself nothing directly to do with ORM. It does help with easily managing transactions and giving you easy access to the entity manager from JPA, which is a standardized ORM solution in Java EE.

Hibernate (3) is indeed an ORM solution, and as it happens one that implements JPA.

So a more logical question is whether to use the standardized JPA interfaces, or to use the Hibernate core API directly. Then a followup question could be whether to use JPA standalone, or in combination with EJB 3.

The answer depends a little on what you need exactly, but typically using JPA in combination with EJB 3 is the easiest solution. Using JPA or Hibernate standalone requires much more verbose code and you manually have to manage transactions, which can be a pain.

JPA vs Hibernate is another debate. JPA has the benefit of having the standardized interfaces, so more developers will likely be familiar with it. On the other hand, the native Hibernate APIs are always a super set of those of JPA and thus offer more power.

Typically developers mainly base their code on JPA, and then use some Hibernate specific annotations or API calls where it makes sense. In 99.99% of the cases such mixed API usage is supported.

Do also note that Glassfish is bundled with EclipseLink, not with Hibernate. EclipseLink is comparable with Hibernate but predates it with more than a decade. Hibernate took a lot from EclipseLink (called TopLink back then).

See also this answer I gave to a similar question: Database table access via JPA Vs. EJB in a Web-Application

Compressive answered 9/1, 2011 at 15:27 Comment(0)
S
5

What you asked is which API is better: EJB3 (JPA) or Hibernate? I said that because you are asking about EJB3 JPA (which is API only) and Hibernate (which is implementation and API). Thus, to compare apples to apples you need to compare APIs. You choose between standard (JPA) and more powerful proprietary API (Hibernate).

But by choosing JPA there is another choice to make: for its implementation. By choosing Hibernate for implementation you can basically discard your question since both JPA and Hibernate are available.

So your question would change: which JPA implementation should I choose (between Hibernate, EclipseLink, OpenJPA, DataNucleus, etc.)?...

Semantic answered 10/1, 2011 at 4:34 Comment(2)
Do note that EJB3 (JPA) might be a little confusing. It suggest that EJB3 is another name for JPA, but this is not the case. It was already technically a completely separate spec in EJB 3.0, and this has been formalized in the current 3.1 version of EJB. JPA is not both technically and organizational a complete different spec. You can't interchange the terms in any way.Compressive
All I meant is to follow terminology in Ehsun's questions. Thanks for clarifications though.Semantic
P
-1

They're similar; the 3.0 spec for EJB took a great deal from Hibernate and Spring.

I don't have any firm metrics for either one to quote, but I'd say that if you're embracing Glassfish that it makes sense to me to use all of its technology. Why introduce another dependency? See if Glassfish can do the job for you.

Parmenter answered 9/1, 2011 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.