aspose file tools*
The moose likes Object Relational Mapping and the fly likes How to create a Template for  EntityManager in a WebApplication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to create a Template for  EntityManager in a WebApplication" Watch "How to create a Template for  EntityManager in a WebApplication" New topic
Author

How to create a Template for EntityManager in a WebApplication

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hello
I am having a web applicaion using JPA for CRUD Operations .
I have defined a Template class for creating and closing EntityManager as shown below :




I have a servlet and want to access EntityManager in it .

So for this i am calling createEntityManager() of Controller .

Please tell me is this the right way to do it .


Save India From Corruption - Anna Hazare.
akhtar qureshi
Greenhorn

Joined: Nov 24, 2009
Posts: 16
No..It is not write way to create EntityManager Object in servlet. Its Throw Exception because em is based on EJB Container So create the EJB project used it. Its a right way...Hope you understand
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

I think you did not understand my question .

I will write a Utitl class static methods for ceating and closing EntityManager and use those methods in my servlet ??

Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

I think you should use EntityManager in EJB..
and then we could easily inject that EJB in our Servlet..


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

if we use EntityManager in EJB..
the instance of EntityManager managed by EJB Container, and so we don't have to deal to open/close the connection and EntityManager..

if you use Spring, then we are forced to write the Util class to deal with the instance of EntityManager..

which one do you use Spring or EJB?..
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi ,

I am using EJB .
I had written a controller class like this .



I am calling these controller static methods inside my EJB Bean class Methods .

Is there anything wrong in this approach .

Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

if you're using EJB, you could easily inject EntityManager in an EJB..

here's the example code :


hope that helps..
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

I tried with that but the problem is that when it is a Container Managed Entity as you said then the transactions can be only of JTA .

Means i cannot have EntityTransaction etr = EntityManager.getTransaction() in my code .

Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

EntityTransaction etr = EntityManager.getTransaction();

its used in local transaction...
there are two kinds of transaction, namely Local transaction and Global Transaction..
and there are two kinds transaction management in EJB, namely Container Manage Transaction (CMT) and Bean Manage Transaction...

the Local Transaction involve only one resource (in this instance its involve 1 database)
the Global Transaction involve several resources (in this instance its involve 1/more database, JMS, and etc)..

you could inject JTA through UserTransaction in EJB for handle transactions..
 
 
subject: How to create a Template for EntityManager in a WebApplication