【发布时间】:2021-07-24 00:33:51
【问题描述】:
在我的 Spring Boot 批处理应用程序中,我正在从 Tasklet 调用 JPA 存储库类。 JPA 调用从 DB 中检索特定值(实体对象)。问题是,如果我更新实体对象中的某些值,一旦控件退出 Tasklet,即使我没有调用任何保存操作,它也会自动更新到 DB。如何防止这种情况?默认的 JPA 实现是 Hibernate。
Tasklet 类
Employee employee = employeeRepository.fetchEmployee(employeeName);
List<Address> addressList = employee.getAddress();
addressList.forEach(e -> e.setStatus(Status.INVALID.toString()));
存储库
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
@Query("select em from Employee em where em.employeeName = :employeeName")
public Employee fetchEmployee(@Param("employeeName") Long employeeName);
}
实体类
@Entity
@Table(name = "Employee")
public class Employee implements java.io.Serializable {
private static final long serialVersionUID = -3769636546619492649L;
private Long id;
private List<Address> address;
private String employeeName;
// Getters and setters
// @OneToMany mapping to Address
}
即使我没有调用 .save() 操作,它也会自动将地址表状态更新为“无效”
【问题讨论】:
-
如果您不想更改,为什么要首先更新值?
-
请分享您的小任务代码以及您用于该小任务的事务管理器的配置。 tasklet 将在 Spring Batch 驱动的事务中执行(请参阅docs.spring.io/spring-batch/docs/4.3.x/reference/html/…),在 JPA 的情况下,如果提交事务,则对附加到持久性上下文的对象的任何更改都可以刷新到数据库。跨度>
标签: java spring-boot jpa spring-batch