如今,我一直在搜索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()方法会使实体分离吗?
发布评论