Forums Register Login

Why we need to persistent unit in EJB 3.0 ?

+Pie Number of slices to send: Send
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 ?
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
That cleared my doubts.

Thanks all of you.
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1544 times.
Similar Threads
Is there a difference between Entity class and Entity beans?
SCBCD 5.0 cleared with 100%
EJB 3.0 and Hibernate
Session beans
Quick question on hierarchical Entity Relationships....
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:45:03.