gpt4 book ai didi

spring - 将分离的实体合并到 JPA 中的实体管理器的最佳方法是什么

转载 作者:行者123 更新时间:2023-12-01 11:36:49 24 4
gpt4 key购买 nike

我正在使用 JPA + spring 开发 Web 应用程序。我的项目层结构是web --> service --> DAO--> GenericDAO。所有类都实例化为单例 spring bean。 GenericDAO 正在使用 @PersistenceContext 注入(inject) entityManager 实例。

我的应用程序在 http session 中获取商店中的域实体。在我对这些实体执行任何数据库操作之前,需要使用合并将其重新附加到实体管理器。我想知道将实体合并到实体管理器的最佳方法。目前每次调用事务方法时,我都会调用 genericDAO.merge(object)。例如

@Transactional
public void addProducts() {
Order order = getOrderFromHttpSession();
genericDAO.merge(order);
// delete existing products
// add new products
// other db operations.
}

还有其他更好的方法吗?是否有任何设计模式适用于此?

最佳答案

我认为这是最好的方法。为此,JPA 提供了这种方法。

    /**
* Merge the state of the given entity into the
* current persistence context.
* @param entity entity instance
* @return the managed instance that the state was merged to
* @throws IllegalArgumentException if instance is not an
* entity or is a removed entity
* @throws TransactionRequiredException if invoked on a
* container-managed entity manager of type
* <code>PersistenceContextType.TRANSACTION</code> and there is
* no transaction
*/
public <T> T merge(T entity);

3.2.4.1 Merging Detached Entity State

The merge operation allows for the propagation of state from detached entities onto persistent entities managed by the EntityManager.

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.

另一种方法是 em.find(someKey) 并复制每个已更改的信息。 (但我不建议你这样做)。最佳实践就是您的解决方案。

关于spring - 将分离的实体合并到 JPA 中的实体管理器的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26111373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com