Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between hibernate and ejb

 
jim li
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by my understanding, i believe that ejb is just api , but hibernate is bunch of concrete classes that implement the ejb api.

so what is the difference between them?


 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not really, Hibernate is just a persistence framework, where you map beans to DB tables, however EJB is much bigger than hibernate and it mainly consists of:

1) persistence layer which are named entity beans.
2) session beans, which contain the business of your operations.
3) message driven beans which are used for Java Messaging.

in addition EJB forces the container to deal with Transaction management, demarcation, resource look up... etc. so when using EJB you will be just seeing business logic and not code used to open sessions, close sessions, manage transactions, timer services ... etc. code is much cleaner and the programmer focus only on the business, and the container does all the job of transactions, sessions etc.

in addition with EJB 3 a new concept of Dependency Injection has been introduced, which makes coding much more easier. don't forget that EJB 3 is a specification and when you are working on big projects (distributed projects) you would feel how much EJB is great.

finally in EJB 3 a new API has been introduced that allows you to map beans to tables using annotations which is the Java Persistence API, and its also used by hibernate . and a new query language somehow similar to HQL but much more powerfull where it allows you to make insert/update queries too.

for my experience EJB 2 is hell, while EJB 3 is really heaven. the only missing thing is the ability of using DI in POJO's too.
 
jim li
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your valuable reply
 
jim li
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do people always combine hibernate and ejb3 to code their application, instead of just using ejb3? since to me, i feel like ebj3 can do everything hibernate does, and it does something extra, such MDB.
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,

Hibernate 3 implements JPA 1.0, which is part of EJB 3.0 (in fact, becoming EJB 3.0 compliant was the major feature for Hibernate 3, including annotation support). In terms of Hibernate specific features that have not been standardized in EJB 3.0, really the criteria API is the most significant, which is being standardized in JPA 2.0. I personally have never had a reason to use the Hibernate criteria API, so do not consider it that important.

As to POJO injection with EJB 3.0, you can use Seam, Guice or even Spring with EJB 3.0 for that.

Hope it helps,
Reza
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic