Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

jpa - EJB 3 or Hibernate 3

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...