JPA的commit()方法会使实体分离吗?

编程入门 行业动态 更新时间:2024-10-13 18:24:33
本文介绍了JPA的commit()方法会使实体分离吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如今,我一直在搜索JPA实体的生命周期. 但是现在,关于实体生命周期还有一些遗漏之处.我在stackoverflow帖子之一中找到了以下图形,请记住该图已被修改.

I have been searching JPA entity life cycle nowadays. But now , there are some missing points about entity life cyle. I have found following graphic in one of stackoverflow posts and keep in mind this diagram had been upvoted.

根据此图,当我们持久化实体时,该实体将变为托管对象. 好的 .没问题 . 当我们提交时,数据进入数据库.好的 . 没问题. 但是图向我们展示了该提交操作使实体脱离了! 让我们看下面的伪代码.

According to this diagram , when we persist entity it becomes managed . OK . No problem . When we commit , data goes to database. OK . No problem. But diagram shows us this commit operation made entity detached ! Let's look at below psuedo code.

entityManager.persist(entity); transactionmit(); // action completed and entity has become detached.(According to the diagram.) entityManager.remove(entity); //Attention this step please .

在上一步(提交步骤)中.那么如何去除分离的对象呢? 如果该实体分离,我们都知道不可能管理分离实体,因为它不再具有与持久性上下文的关联.

in previous step(commit step). So how is it possible to remove a detached object? If this entity becomes detached , we all know it is not possible to manage a detached entity since it has no assocaiton with persistence context anymore.

那么如何删除分离的对象呢?在这一点上,你能请我澄清一下吗? 预先感谢!

So how is it possible to remove a detached object? Could you please clarify me at this point ? Thanks in advance !

推荐答案

实体可以通过以下方式之一分离(可能有更多方式):

Entity can become detached in one of the following ways (there could be more ways):

  • 在事务(在事务范围内的持久性上下文中)提交时,由持久性上下文管理的实体将分离.

  • When the transaction (in transaction-scoped persistence context) commits, entities managed by the persistence context become detached.

    如果关闭了应用程序管理的持久性上下文,则所有托管实体都将分离.

    If an application-managed persistence context is closed, all managed entities become detached.

    使用清除方法

    使用分离方法

    回滚

    在扩展的持久性上下文中,当删除有状态的Bean时,所有受管实体都将被分离.

    In extended persistence context when a stateful bean is removed, all managed entities become detached.

    我认为问题可能出在应用程序管理的,用户管理的,扩展的持久性上下文之间.

    I think the problem could be the difference between application managed, user managed, extended persistence contexts.

  • 更多推荐

    JPA的commit()方法会使实体分离吗?

    本文发布于:2023-10-24 20:29:50,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1524933.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:会使   实体   方法   JPA   commit

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!