In a seam-gen generate开发者_StackOverflow社区d application, I have a user entity which is check during authentication. I lookup in the authenticator whether the entity is available, if yes I load the Entity in the UserHome.find()
method.
If it is not available I want to force the user to register. This works so far.
But in subsequent calls if I need to use the user entity again. The Entity becomes 'unmanaged' . I tried to Outject it into Scope.Session but this didn't help either.
Each call to EntityHome.isManaged()
throws an Exception since the EntityManager is already closed.
The question is who closes the entityManager, and why?.
Is there a way to prevent seam from closing the entityManager other than using :
@PersistenceContext(type=PersistenceContextType.EXTENDED) EntityManager entityManager;
The entityManager is conversation-scoped. If you do not start a conversation (with @Begin
) in a request, Seam closes the entityManager after the request. Outjecting the entity into the session scope doesn't help because the entityManager closes anyway.
To prevent Seam to close the entityManager you either use an extended persistence context as you mentioned yourself or work with a conversation-scoped component. Depending on the context of your component, and subsequent calls respectively, using the conversation scope might not be appropriate.
If you outject the entity to the session scope. You can re-attach it to the new entityManager in subsequent calls by using entityManager.find(.)
or entityManager.getReference(.)
.
What @kraftan said, and I want to add the following:
Use the seam managed persistence context either by injection
@In EntityManager entityManager;
if you in components.xml have defined
<persistence:managed-persistence-context
name="entityManager" auto-create="true" persistence-unit-jndi-name="@puJndiName@" />
Or, if you are inside Seam Application framework, you can just say getEntityManger()
and you can work with the seam's managed entitymanager
精彩评论