获取对象字段previous价值休眠JPA [英] Getting object field previous value hibernate JPA

查看:113
本文介绍了获取对象字段previous价值休眠JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有这个类:

Let's assume I have this class :

@EntityListeners({MyListener.class})
class MyClass {
  String name;
  String surname;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name; 
  }

  public String getSurname() {
    return name;
  }

  public void setSurname(String name) {
    this.name = name; 
  }

  public void save() {
    JPA.em().persist(this);
    return this;
  }

  public void update() {
    JPA.em().merge(this);
  }

  public static MyClass findById(Long id) {
    return JPA.em().find(MyClass.class, id);
  }
}

现在在我的 myListener的类我试图找出previous值 MyClass的实例对比新值,即将得到保存(更新)到数据库中。我这样做与preupdate metdhod:

Now in my MyListener class I'm trying to figure out the previous value MyClass instance versus the new value that is about to get saved(updated) to database. I do this with preupdate metdhod :

@PreUpdate
public void preUpdate(Object entity) {
...some logic here...unable to get the old object value in here           
}

因此​​,假设我有一个 MyClass的对象实例姓名:

MyClass mycls = new MyClass();
mycls.setName("Bob");
mycls.setSurname("Dylan");
mycls.save();

这不是由监听器是好的,因为我听只更新回升。
现在,如果我要更新这个例子,像这样:

This wasn't picked up by listener which is ok because I'm listening only to updates. Now if I were to update this instance like so :

MyClass cls = MyClass.findById(someLongId)
cls.setSurname("Marley");
cls.update();

因此​​,这触发了myListener的 preUpdate 方法,并当我尝试:

MyClass.findById(someLongId);

当我调试,我已经得到了最新更新的实例,但因为当我在列姓数据库检查更新还没有发生,它仍然迪伦

When I debug I already get the newly updated instance but the update hasn't happened yet because when I check in database in column surname it's still Dylan.

我如何从数据库在我的 preUpdate 方法获得的价值,而不是我刚刚更新了吗?

How do I get the value from database in my preUpdate method and not the one I just updated?

推荐答案

我想一个简单的方法是保存在一个短暂的变量previous值,JPA不会持续。
所以刚才介绍的变量previousSurname和保存的实际值您在二传手覆盖它之前。

I think an easy way is to save the previous value in a transient variable that JPA will not persist. So just introduce a variable previousSurname and save the actual value before you overwrite it in the setter.

如果你想保存多个属性会很容易,如果你的类 MyClass的序列化。

If you want to save multiple properties it would be easy if your class MyClass is Serializable.

如果这样增加后负荷监听器

If so add a post load listener

public class MyClass implements Serializable {

     @Transient
     private transient MyClass savedState;

     @PostLoad
     private void safeState(){
        this.savedState = SerializationUtils.clone(this); // from apache commons-lang
     }

}

但要知道,在 savedState 是一个超然的实例。

But be aware that the savedState is a detached instance.

您可以然后访问在previous状态的 EntityListener

You can then access the previous state in your EntityListener.

您还可以将餐后监听器 EntityListener 类。但你需要访问 savedState 字段。我建议,使其无论是包范围或使用包范围的访问,并把 MyClass的 myListener的在同一个包,

You can also move the PostLoad listener to the EntityListener class. But then you need access to the savedState field. I recommend to make it either package scoped or use a package scoped accessor and put MyClass and MyListener in the same package,

public class MyListener {

    @PostLoad
    private void safeState(MyClass myClass){
         myClass.safeState(SerializationUtils.clone(myClass)); // from apache commons-lang
    }

}

public class MyClass implements Serializable {

     @Transient
     private transient MyClass savedState;

     void safeState(MyClass safedState){
        this.savedState = safedState;
     }

}

这篇关于获取对象字段previous价值休眠JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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