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?
EJB is still there and growing up. There are many new features (SOAP/RESTful webservice, JPA entities, JAXB...)
EJB is an Enterprise Java Bean -- see link for description, but basically its the 'default' java way of writing an enterprise application. Hibernate is an ORM Framework; a way to map the Objects/Classes in your application to database tables. It is related to how you persist your data to a database.
Is JPA part of EJB 3.0 ? Yes and no... Yes because every application server claiming to implement EJB 3.0 spec must also provide JPA implementation. No because JPA can be easily outside of EJB, in standalone applications or Spring-managed ones.
Introducing EJB3 Persistence The EJB3 specification recognizes the interest and the success of the transparent object/relational mapping paradigm. The EJB3 specification standardizes the basic APIs and the metadata needed for any object/relational persistence mechanism.
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
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.)?...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With