java - 如何保留分离的对象?

标签 java hibernate jpa-2.0

我有一个简单的编辑器 UI,用于对数据库表执行 CRUD。 在 Hibernate 5.1 下,过程是:

  1. 创建 session 。读入数据对象。关闭 session ,分离对象。
  2. 使用分离的对象填充 UI 小部件。
  3. 允许用户在 UI 中添加/编辑/删除条目,修改分离的对象。
  4. 在用户“保存”操作时:创建一个新 session 。使用 saveOrUpdate() 保存新的/更新的对象。结束 session 。
  5. 根据需要重复。

Hibernate 5.2 强烈建议从 Hibernate-native API 转移到 JPA API。 JPA 不允许您重新附加分离的对象。

有几个解决方法:

  1. 使用 unwrap 从 JPA EntityManager 获取 Hibernate session ,并使用它调用 saveOrUpdate。我不喜欢这个选项,因为它依赖于不属于 JPA 的功能。
  2. 使用 JPA 合并,这将更新持久对象,但我必须确保我不会破坏任何关联。这为我提供了同一对象的 2 个副本,一个保留,一个分离......这很困惑。
  3. 执行手动合并操作,将修改后的字段从分离对象复制到持久对象。这是额外的工作。
  4. 在整个过程中保持单个 EntityManager 实例处于 Activity 状态。不幸的是,其他线程可以在该 session 仍处于打开状态时执行 CRUD 操作,从而使持久化上下文与数据库表不同步。所以我也不喜欢这种方法。

有什么好的方法可以做到这一点,或者这些是唯一可用的选择吗?

最佳答案

JPA does not allow you to reattach detached objects.

JPA 规范定义了merge() 操作。该操作似乎对实现所描述的用例很有用。

请引用规范:

3.2.7.1 Merging Detached Entity State

The merge operation allows for the propagation of state from detached entities onto persistent entities managed by the entity manager. The semantics of the merge operation applied to an entity X are as follows:

  • If X is a detached entity, the state of X is copied onto a pre-existing managed entity instance X' of the same identity or a new managed copy X' of X is created.
  • If X is a new entity instance, a new managed entity instance X' is created and the state of X is copied into the new managed entity instance X'.
  • If X is a removed entity instance, an IllegalArgumentException will be thrown by the merge operation (or the transaction commit will fail).
  • If X is a managed entity, it is ignored by the merge operation, however, the merge operation is cascaded to entities referenced by relationships from X if these relationships have been annotated with the cascade element value cascade=MERGE or cascade=ALL annotation.
  • For all entities Y referenced by relationships from X having the cascade element value cascade=MERGE or cascade=ALL, Y is merged recursively as Y'. For all such Y referenced by X, X' is set to reference Y'. (Note that if X is managed then X is the same object as X'.)
  • If X is an entity merged to X', with a reference to another entity Y, where cascade=MERGE or cascade=ALL is not specified, then navigation of the same association from X' yields a reference to a managed object Y' with the same persistent identity as Y.

The persistence provider must not merge fields marked LAZY that have not been fetched: it must ignore such fields when merging.

Any Version columns used by the entity must be checked by the persistence runtime implementation during the merge operation and/or at flush or commit time. In the absence of Version columns there is no additional version checking done by the persistence provider runtime during the merge operation.

JSR 338: JavaTM Persistence API, Version 2.1, Final Release.

关于java - 如何保留分离的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45734475/

相关文章:

JavaCL - 管理非常大的图像处理

java - Eclipse 仅应用一种格式规则

hibernate - JPA createQuery 或 @NamedQuery

java - 对行的某种类型实现分页

java - JComboBox 在单击之前不显示

java - 如何使溢出的TextArea截断文本并在最后显示省略号?

java - 试图删除 Entitymanager,但没有设置

Java Spring - 使用 API 持久化对象可以工作,但我无法在测试中使用服务持久化对象,空指针

java - Hibernate 搜索索引单个租户

java - JPA : Can we use different collection type on either sides of ManyToMany relationship?