如果我坚持后分离,为什么找不到实体

发布于 2024-12-08 12:57:36 字数 962 浏览 1 评论 0原文

我有带有容器管理事务的无状态会话 Bean。我想在数据库中创建(保留)非托管实体后返回它。我就是这样做的:

@Stateless
public class MyBean {

   @EJB(name="MyController")
   private MyController myController;

   public MyEntity create(MyEntity entity) {
      //...
      myController.create(entity);
      myController.preTransfer(entity);
      return entity;
   }
}
@Stateless
public class MyController {

   @PersistenceContext(unitName = "myPU")
   private EntityManager em;

   public void create(MyEntity entity) {
      //...
      em.persist(entity);
   }

   public void preTransfer(MyEntity entity) {
       if (em.contains(entity)) {
           em.detach(entity);
       }
       //...
   }
}

我调用 MyBean.create,实体成功持久化,并且 MyBean.create 返回非托管实体,没关系。但下次当我尝试通过 id 检索该实体时,却找不到它。如果我评论分离,可以找到实体,但MyBean.create在这种情况下返回托管实体。我哪里错了?

I have Stateless Session Bean with Container-Managed Transactions. I want to return unmanaged entity after create (persist) it in a database. That's how I do it:

@Stateless
public class MyBean {

   @EJB(name="MyController")
   private MyController myController;

   public MyEntity create(MyEntity entity) {
      //...
      myController.create(entity);
      myController.preTransfer(entity);
      return entity;
   }
}
@Stateless
public class MyController {

   @PersistenceContext(unitName = "myPU")
   private EntityManager em;

   public void create(MyEntity entity) {
      //...
      em.persist(entity);
   }

   public void preTransfer(MyEntity entity) {
       if (em.contains(entity)) {
           em.detach(entity);
       }
       //...
   }
}

I call MyBean.create, entity successfully persisted and MyBean.create return unmanaged entity, that's ok. But next time when I try retrieve this entity by id, it can't be found. If I comment detach, entity can be found, but MyBean.create return managed entity in that case. Where I am wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝咒 2024-12-15 12:57:36

EntityManager.detach 状态:

未刷新对实体所做的更改(如果有)(包括删除
实体),不会同步到数据库

所以你要持久化它,然后分离它。但与 persist 相关的操作尚未刷新,因此该实体尚未保存到数据库中。

为什么要拆开它?一旦交易结束,它将自动分离。

The javadoc of EntityManager.detach states:

Unflushed changes made to the entity if any (including removal of the
entity), will not be synchronized to the database

So you're persisting it, then detaching it. But the operations associated to persist have not been flushed yet, and the entity is thus not saved to the database.

Why do you want to detach it? As soon as the transaction is ended, it will be detached automatically.

雨落星ぅ辰 2024-12-15 12:57:36

修改 JB 的答案:您也可以使用干净的标准机制,而不强制容器显式分离或刷新:

@Stateless
public class MyController {

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public MyEntity create(MyEntity entity) {
       //...
       em.persist(entity);
       return entity;
    }
}

结果您将得到一个分离的实体。

Amending JB's answer: you could as well use clean, standard mechanism, without forcing the container to detach or flush explicitly:

@Stateless
public class MyController {

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public MyEntity create(MyEntity entity) {
       //...
       em.persist(entity);
       return entity;
    }
}

You'll get a detached entity as a result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文