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

How to create a Template for EntityManager in a WebApplication

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
akhtar qureshi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should use EntityManager in EJB..
and then we could easily inject that EJB in our Servlet..
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic