如何在@HandleBeforeSave事件中获取旧实体值以确定属性是否被更改? [英] How to get old entity value in @HandleBeforeSave event to determine if a property is changed or not?

查看:509
本文介绍了如何在@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());




$ b $ p
$ b

如果我尝试使用旧实体 findOne 方法,但是这会返回新事件。可能是因为当前会话中存在Hibernate / Repository缓存。



有没有办法让旧实体?



<我需要这个来确定一个给定的属性是否被改变。如果属性发生变化,我需要执行一些操作。

解决方案

您目前正在使用Spring数据抽象。
如果find返回新值,spring-data显然已经将该对象附加到hibernate会话中。



我认为你有三个选择: p>


  1. 在刷新当前季节之前,在单独的会话/事务中获取对象。这很尴尬,需要非常微妙的配置。

  2. 在spring附加新对象之前获取以前的版本。这是相当可行的。在将对象交给资源库之前,您可以在服务层中完成它。你可以,但不能保存一个对象也是一个hibernate会话时,另一个感染相同的类型和ID我们知道。在这种情况下,使用 merge evict
  3. 使用较低级别的hibernate拦截器,如此处所述。正如你看到 onFlushDirty 具有两个值作为参数。但请注意,hibernate通常不会查询以前的状态,只需保存一个已经存在的实体即可。而不是在db中发出一个简单的更新(不选)。您可以通过在实体上配置select-before-update来强制选择。


I'm trying to get the old entity in a @HandleBeforeSave event.

@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());
    }
}

In the event I try to get the old entity using the findOne method but this return the new event. Probably because of Hibernate/Repository caching in the current session.

Is there a way to get the old entity?

I need this to determine if a given property is changed or not. In case the property is changes I need to perform some action.

解决方案

You're currently using a spring-data abstraction over hibernate. If the find returns the new values, spring-data has apparently already attached the object to the hibernate session.

I think you have three options:

  1. Fetch the object in a separate session/transaction before the current season is flushed. This is awkward and requires very subtle configuration.
  2. Fetch the previous version before spring attached the new object. This is quite doable. You could do it in the service layer before handing the object to the repository. You can, however not save an object too an hibernate session when another infect with the same type and id it's known to our. Use merge or evict in that case.
  3. Use a lower level hibernate interceptor as described here. As you see the onFlushDirty has both values as parameters. Take note though, that hibernate normally does not query for previous state of you simply save an already persisted entity. In stead a simple update is issued in the db (no select). You can force the select by configuring select-before-update on your entity.

这篇关于如何在@HandleBeforeSave事件中获取旧实体值以确定属性是否被更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆