分离实体和托管实体
“独立实体”是什么意思? 如何在事务期间将托管实体转换为分离实体?
What a "detached entity" means?
How is it possible to convert a managed entity to a detached entity during a transaction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
分离实体是其状态不得由 JPA 提供者反映的实体。
换句话说,如果您更改其状态(即通过 setters 方法),这些更改将不会保存到底层数据库,因为 JPA 提供程序不必“观察”此类实体。
如果实体 E1 是托管实体,您可以通过调用(非常合理的命名)方法
EntityManager#detach(E1)
。您还可以使用EntityManager# clear()
它将清除整个 PersistenceContext 并有效地使所有托管实体分离。A detached entity is an entity whose state must not be reflected by the JPA provider.
In other words, if you change its state (i.e. through setters methods) these changes will not be saved to the underlying database, as the JPA provider doesn't have to "observe" such entities.
If entity E1 is a managed entity you can make it detached invoking (very reasonable named) method
EntityManager#detach(E1)
. You can also useEntityManager#clear()
which will clear whole PersistenceContext and effectively making all managed entities detached.分离的实体对象是处于特殊状态的对象,在这种状态下,它们不受任何 EntityManager 管理,但仍然代表数据库中的对象。了解更多来源
阅读此处
Detached entity objects are objects in a special state in which they are not managed by any
EntityManager
but still represent objects in the database. Read more sourceRead here
在这里您可以阅读有关 JPA 实体生命周期的信息。
例如,可以在序列化或关闭持久性上下文之后分离实体。
Here you can read about JPA entity lifecycle.
Entity can be detached after serializing or closing of Persistence Context, for example.