-
1. Re: Different entitymanagers with EJBs and Seam EM
norman Mar 30, 2009 9:25 PM (in response to trouby)I'm not sure what your question is, but using an app server managed persistence context and a Seam-managed persistence context are two completely different things.
-
2. Re: Different entitymanagers with EJBs and Seam EM
trouby Mar 31, 2009 12:18 AM (in response to trouby)Let me explain,
I have a general EJB that manages few things (sometimes invoked via Seam code, sometimes not)
But when I'm inside seam context and use EJB, I loose the conversational enhancements, and gets a lot of Lazy fetch exceptions, I set Seam's entityManager sometimes manually into the EJB and then invoke its methods that way my EJB logic stays the same and enhanced with Seam's EM when neccessary.So all the methods in the EJB retrieve entityManger by invoking the getEntityMangaer() method that looks like that:
@PersistenceUnit private EntityManagerFactory factory; public EntityManager getEntityManager() { if ( (entityManager == null) || (!entityManager.isOpen()) ) { entityManager = factory.createEntityManager(); } return entityManager; }
But as it seems, the behavior of factory.createEntityMangaer() is different then the behavior when using:
@PersistenceContext public EntityManager entityManager;
That makes AS inject the entityManager by its own, as at the moment I started to use 'factory.' I'm getting errors related to a usage of different entityManagers. again, probably the AS is smart enough to inject the correct one that contains my managed entities.
I'm seeking for a replacement of the factory that simulates the way the AS injects the entityManager but by code and not via the annotation,
Thanks,
Asaf.
-
3. Re: Different entitymanagers with EJBs and Seam EM
gonorrhea Apr 1, 2009 6:13 AM (in response to trouby)use @In to inject Seam container managed persistence context (SMPC).
use @PersistenceContext to inject EJB container managed persistence context.
AFAIK, your EJB implementation is independent of what method you use for EntityManager injection as the injection method is merely metadata (in this case annotations).
Are you saying you sometimes need to use @In and sometimes use @PersistenceContext?
Why do you need to even use @PersistenceUnit? That annotation is not even referenced in the Seam 2.1.1.GA ref doc.
it's configured like this in components.xml:
<persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/fooEntityManagerFactory"/>
-
4. Re: Different entitymanagers with EJBs and Seam EM
swd847 Apr 1, 2009 6:27 AM (in response to trouby)The Application server will inject a transaction scoped EntityManager, you are getting a brand new entityManager, which is why you are having problems. If you absolutely must get an entityManager programatically you have to look it up in JNDI, the spec has the following example:
@Stateless @PersistenceContext(name="OrderEM") public class MySessionBean implements MyInterface { @Resource SessionContext ctx; public void doSomething() { EntityManager em = (EntityManager)ctx.lookup("OrderEM"); ... } }