我正在尝试在@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
方法获取旧实体,但是返回了新的实体。这可能是由于当前会话中的Hibernate / Repository缓存引起的。
有没有办法获取旧实体?
我需要这个来确定给定属性是否已更改。如果属性发生更改,则需要执行某些操作。