Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why we need to persistent unit in EJB 3.0 ?

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to EJB 3.0 and was going though the theory part of the persistent unit and transaction.

And i was wondering why we need persistent unit !!! Why can't we just begin a transaction, persist set of entities and commit the transaction.
Logically all the entities should be part of the persistent unit right ?

Is persistent.xml in EJB 3 is like hibernate.cfg.xml in Hibernate ?

Why do we need to provide name of the persistent unit when creating EntityManager object ?
 
Louie van Bommel
Ranch Hand
Posts: 76
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a lot of questions here, so I'm going to take a stab at the one in the title. A persistence unit is needed to tell the EntityManager which class you need to persist. Now I think you might be asking why did the designers of JPA decide to make it necessary to have a persistence unit. If that's your question, then my answer is that it makes your application more resilient to changes to persistence providers. For example, if you used JPA and coded your application to use Derby as the database, switching it over to MySQL as a database would be simple; it's just a matter of specifying MySQL in one configuration file (persistence.xml). I'll leave the other parts of your question for others.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPA defines a persistence interface. The developer is free to choose different providers (implementors) of that interface of which, Hibernate is one choice but there are others.
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jigar Naik wrote:Why can't we just begin a transaction, persist set of entities and commit the transaction.

Because without the persistence.xml file, the container won't know which entities can be persisted.
Logically all the entities should be part of the persistent unit right?

This is a smart question, but it's not always the case that all entities will be persisted. I work on a modular application, and unless a client purchases all modules, they won't have all of the entities. E.g., if you have a system that has both accounting and production functions, and someone buys the production module, you wouldn't want them to have access to the entities belonging to the accounting module.
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That cleared my doubts.

Thanks all of you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic