• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

detached entity passed to persist

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using JPA for persistence. Since I am on EJB 2.1 am not able to avail injection technique for getting EntityManager object. Below is my code snippet:



But I get an excpetion "detached entity passed to persist".

I tried to browse the information on this but it looks like the people who could solve this problem are using injected entity manager.

Could you please explain how can I resolve this error?

Please note that I am successfully able to run all the remaining APIs like merge, createQuery, find, etc.. with the entity manager on the same Person entity.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you don't have injection, you also don't get the transaction. You need to get a Transation from the Entity Manager and start it up, then at the end commit the transaction.

Mark
 
Amol Katyare
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the confirmation. Can I use injection technique with EJB 2.1 and/or in web only project? I am on EJB 2.1 and I tried to use injection technique but somehow entity manager is not getting injected by container and I get Null pointer exception.


Could you please throw some light on this and how I would be able to use transaction in EJB 2.1?
 
Amol Katyare
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any reply please ??
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amol Katyare:
Thanks for the confirmation. Can I use injection technique with EJB 2.1 and/or in web only project? I am on EJB 2.1 and I tried to use injection technique but somehow entity manager is not getting injected by container and I get Null pointer exception.


Could you please throw some light on this and how I would be able to use transaction in EJB 2.1?



No, EJB 2.1 has no concept of Dependency Injection.

In EJB 2.1 you just use the other technique of calling Transaction t = session.beginTransaction();

then when you want to commit or rollback you call the method on Transaction class.

Mark
Mark
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic