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 ?
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.
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