store spring JPA 存储库在保存前查找数据到变量?

store spring JPA repository find data to variable before save?

我想在保存之前存储 spring JPA 存储库对象。

在功能更新中,我想将旧记录数据存储到 dbDomain 但在 repository.save(updated) 之后,dbDomain 突然更改为新的更新数据。

有人知道怎么解决吗?谢谢

package com.test.admintool.userauth.services;

import java.text.SimpleDateFormat;
import java.util.*;

import com.test.admintool.engine.common.AdminToolFunction;
import com.test.admintool.engine.common.AuditTrailMessage;
import com.test.admintool.engine.common.CopyUtil;
import com.test.admintool.engine.service.AdminService;
import com.test.admintool.engine.service.AuditTrailEventService;
import com.test.admintool.engine.utils.SecurityUtil;
import com.test.admintool.userauth.entity.User;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.GenericTypeResolver;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

import com.test.admintool.engine.AdminEntity;
import com.test.admintool.engine.exception.EntityNotFoundException;
import com.test.admintool.engine.repository.AdminRepository;
import com.querydsl.core.types.Predicate;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Table;

@Slf4j
public abstract class AdminServiceImpl<T extends AdminEntity<T>> implements AdminService<T> {

    private static final String AUDIT_ACTION_CREATE = "CREATE";
    private static final String AUDIT_ACTION_UPDATE = "UPDATE";
    private static final String AUDIT_ACTION_DELETE = "DELETE";
    private final AdminRepository<T> repository;

    private final AuditTrailEventService auditTrailEventService;

    protected AdminServiceImpl(AdminRepository<T> repository, AuditTrailEventService auditTrailEventService) {
        this.repository = repository;
        this.auditTrailEventService = auditTrailEventService;
    }

    public T get(Long id) throws EntityNotFoundException {
        return repository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException(String.format("Product %s not found", id)));
    }

    @Transactional
    public T update(T updated) throws EntityNotFoundException {
        T dbDomain = get(updated.getId());
        T updatedData = repository.save(updated);
        produceAuditMessageUpdate(dbDomain, updatedData, AUDIT_ACTION_UPDATE);
        return updatedData;
    }
}

我假设你正在传递一个detached实体实例给update方法,并且在进入该方法时启动一个新事务(通过spring 事务性 aop 代理)。
(否则,dbDomain 条目将从 hibernate 一级缓存解析为传递的实体实例 updated,这样就不会有任何 属性 差异。)

当将 detached 实例 updated 传递给 save 方法时,实体管理器会将其合并到持久性上下文中。因此,它还将检查同一实体(相同 ID)的另一个已附加实例,并通过将所有值从新传递的实例复制到另一个附加实例来保持实体的另一个实例同步:
https://github.com/hibernate/hibernate-orm/blob/734b80c531bdb2a9bb33971cd57f51fce662e758/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultMergeEventListener.java#L346
(这是有道理的,因为否则您最终会在附加实体上得到不同的值,并且您无法决定在事务结束时将哪些值持久保存到数据库中。)

为了防止您的 dbDomain 实例被更新,您可以在调用 save(updated):

之前手动将其从持久性上下文中分离出来
T dbDomain = get(updated.getId());
entityManager.detach(dbDomain); // <--
T updatedData = repository.save(updated);    

或者,您也可以 deep-copy 相关的 dbDomain 属性 值。