-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Straight answer: EntityManager across Session Beans?
PostPosted: Mon Jan 11, 2010 7:59 pm 
Newbie

Joined: Mon Dec 28, 2009 10:56 pm
Posts: 5
Hi guys,

I'm having a really hard time getting a straight answer to this question. There appears to be no doco anywhere on it. I will happily blog this for other people if somebody can resolve it for me!

If I have two session beans, Foo and Bar, that both use @Resource to inject an EntityManager...

Code:
@Stateful
class Foo {
   @Resource EntityManager mEntityManager;
   public void save( Company company ) {
      getBean( Bar ).save( company );   // [1]
     
      Company parentCompany = mEntityManager.find( parentCompanyId );
      parentCompany.add( company );   // [3]
   }
}

@Stateful
class Bar {
   @Resource EntityManager mEntityManager;
   public void save( Company company ) {
      mEntityManager.merge( company );   // [2]
   }
}

...such that...

1. Foo passes a (new) EntityBean to Bar
2. Bar saves it using its EntityManager
3. Foo adds it to a PersistentSet that cascades

...then I get all sorts of problems. @Resource appears to inject two separate EntityManagers, so they get very confused as they both try to persist company upon flush (one because of the explicit .merge, one because of the implicit cascade).

My question is:

Should you not try to use an EntityManager 'across beans'? Is there a way to have JPA inject the same EntityManager into both beans so that the transaction is coherent?

Regards,

Richard.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.