我正在尝试获取旧实体@HandleBeforeSave。
@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缓存。
findOne
有没有办法获得旧实体?
我需要此以确定给定属性是否已更改。如果属性是更改,则需要执行一些操作。
您目前在hibernate状态下使用spring-data抽象。如果查找返回新值,则表明spring-data已将对象附加到hibernate会话。
我认为您有三种选择:
save
merge
evict
onFlushDirty