Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EntityManager types in JPA

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

The Pro EJB 3 book dictates on page 112 that the JPA defines no less than 3 different types of EntityManager. I can see only two of them, namely the Container Managed Entity Manager and the Application Managed Entity Manager. Which is the third one? Or is that simply a typo from the book?
 
Witold Koszulinski
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jothi,

There are two container-managed entity managers. The first one is transaction-scoped entity manager and the second one is extended entity manager. Both described on page 112 and on page 113. There is also application-managed entity manager described on page 117.

Regards
Witold
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The extended and transaction scoped are subtypes for the Container Managed Entity Manager. But what about Application Managed Entity Manager. Can't they also be transaction scoped or extended scoped? Correct me if I'm wrong!
 
Uppala Ramana
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The extended and transaction scoped are subtypes for the Container Managed Entity Manager. But what about Application Managed Entity Manager. Can't they also be transaction scoped or extended scoped? Correct me if I'm wrong


Hi Jothi,

There are two ways of using the EntityManager

1) Container Managed ::: Using Dependency Injection ...so the container is responsible for initializing the EntityManager
@PersistenceContext
EntityManager entityManager;

2) Application Managed::: User or Application is Responsible for creating and initializing the EntityManager

EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyUnit")
EntityManager em = emf.createEntityManager();

Now coming back to Transaction or Extended ..those are the Persistence Context Types.
Container Managed Entity Manager can have any of the below Persistent Context Type

@PersistenceContext(type=PersistenceContextType.TRANSACTION)
EntityManager entityManager;


@PersistenceContext(type=PersistenceContextType.EXTENDED)
EntityManager em;

As far as i know, for the Application Managed Entity Manager ..application is responsible for managing the transactions.

But what about Application Managed Entity Manager. Can't they also be transaction scoped or extended scoped? Correct me if I'm wrong!


That dependes on the application code...i think there is no meaning of extended scope for the application managed entity manager.
 
Logan Lee
Greenhorn
Posts: 18
Java Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what about Application Managed Entity Manager. Can't they also be transaction scoped or extended scoped? Correct me if I'm wrong!


Only container managed contexts can be transaction-scoped, so only Entity Managers that are injected with the @PersistenceContext annotation or it's XML equivalent may be transaction-scoped.
 
Singh Harmeet
Ranch Hand
Posts: 115
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uppala Ramana explain the three types of JPA transaction . but

and

are the same , because by default default value is so , how we said that there is three type of JPA entity managers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic