如何在@HandleBeforeSave事件中获取旧实体值以确定属性是否已更改?
2022-09-01 13:18:18
我正在尝试在事件中获取旧实体。@HandleBeforeSave
@Component
@RepositoryEventHandler(Customer.class)
public class CustomerEventHandler {
private CustomerRepository customerRepository;
@Autowired
public CustomerEventHandler(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
@HandleBeforeSave
public void handleBeforeSave(Customer customer) {
System.out.println("handleBeforeSave :: customer.id = " + customer.getId());
System.out.println("handleBeforeSave :: new customer.name = " + customer.getName());
Customer old = customerRepository.findOne(customer.getId());
System.out.println("handleBeforeSave :: new customer.name = " + customer.getName());
System.out.println("handleBeforeSave :: old customer.name = " + old.getName());
}
}
在这种情况下,我尝试使用该方法获取旧实体,但这会返回新事件。可能是因为当前会话中的休眠/存储库缓存。findOne
有没有办法获得旧实体?
我需要这个来确定给定的属性是否被更改。如果属性发生变化,我需要执行一些操作。