• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to create a Template for EntityManager in a WebApplication

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??

 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic