Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate and JPA, what to use, where?

Could someone please explain to me what the main differences are between JPA and Hibernate?

Where to use Hibernate ?

Where to use JPA?

Why not entity bean?

like image 891
Isabel Jinson Avatar asked Jun 24 '10 10:06

Isabel Jinson


1 Answers

A little history:

Entity beans were part of EJB 1 and 2. They were hell to work with, so an alternative was due. Then Hibernate appeared. (I don't remember these times)

Hibernate evolved to be a de-facto standard in object-relational mapping. Then it was decided that a standard is needed, so the JPA specification was created, highly influenced by Hibernate.

JPA is just a specification - it defines what an ORM framework should do, and what annotations should it support. JPA is implemented by many vendors - Hibernate, EclipseLink, OpenJPA, etc.

So:

  • don't use Entity beans
  • use any JPA implementation that you like. Hibernate is definitely a good choice.

Update: About your secondary question in the comments:

Yes, you can use JPA with EJB Session beans:

@Stateless
public class YourSessionBean implements RemoteInterface {

     @PersistenceContext
     private EntityManager entityManager; // this is the JPA EntityManager
}

And you have the entity manager injected by the container and ready to operate JPA entities. Of course you'd need to make configurations for that, but that's out of the scope of this question.

like image 51
Bozho Avatar answered Oct 29 '22 07:10

Bozho