File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Why we need to persistent unit in EJB 3.0 ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Why we need to persistent unit in EJB 3.0 ?" Watch "Why we need to persistent unit in EJB 3.0 ?" New topic
Author

Why we need to persistent unit in EJB 3.0 ?

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 751
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 ?


Jigar Naik


Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
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

Joined: Nov 09, 2011
Posts: 973
    
    5

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

Joined: Apr 23, 2009
Posts: 157

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.


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 751
That cleared my doubts.

Thanks all of you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why we need to persistent unit in EJB 3.0 ?
 
Similar Threads
Is there a difference between Entity class and Entity beans?
Quick question on hierarchical Entity Relationships....
EJB 3.0 and Hibernate
SCBCD 5.0 cleared with 100%
Session beans