[英]Injected EntityManager's scope/lifecycle in a Stateless bean
Can someone explain the lifecycle of an injected EntityManager in a stateless bean? 有人可以解释无状态Bean中注入的EntityManager的生命周期吗? If a stateless bean has an injected EntityManager associated with a specific PersistenceContext, what happens to that association the second time the bean is used?
如果无状态bean具有与特定PersistenceContext关联的注入的EntityManager,那么第二次使用该bean时该关联会发生什么?
For example, I have the following: 例如,我有以下内容:
@Stateless
public class TimeStepsBean
{
@PersistenceContext(unitName="DynamicDB")
private EntityManager em;
public List<TimeStep> timeSteps = new ArrayList<TimeStep>();
private void init()
{
if (timeSteps.isEmpty())
{
TypedQuery<TimeStep> query = em.createQuery("SELECT t FROM TimeStep t", TimeStep.class);
timeSteps = query.getResultList();.
}
}
public void refreshSteps()
{
init();
em.flush();
em.refresh(timeSteps.get(0));
}
}
When refreshSteps is called the second time the bean is used (a second transaction), I get a "java.lang.IllegalArgumentException: Entity not managed". 当第二次使用bean(第二次事务)时调用refreshSteps时,我得到一个“ java.lang.IllegalArgumentException:实体不受管理”。 The entityManager was injected, so I am assuming that it is always part of the current persistence context.
注入了entityManager,因此我假设它始终是当前持久性上下文的一部分。 Is that true?
真的吗?
Adding em.merge(timeSteps.get(0)) before the refresh still generates the same exception. 在刷新之前添加em.merge(timeSteps.get(0))仍然会生成相同的异常。
Your timeSteps is a state. 您的timeSteps是一种状态。 Your class is annotated as stateless.
您的班级被注释为无状态。 It's a misuse of the framework.
这是对框架的滥用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.