<tfoot id='agJHA'></tfoot>

        <bdo id='agJHA'></bdo><ul id='agJHA'></ul>

    1. <i id='agJHA'><tr id='agJHA'><dt id='agJHA'><q id='agJHA'><span id='agJHA'><b id='agJHA'><form id='agJHA'><ins id='agJHA'></ins><ul id='agJHA'></ul><sub id='agJHA'></sub></form><legend id='agJHA'></legend><bdo id='agJHA'><pre id='agJHA'><center id='agJHA'></center></pre></bdo></b><th id='agJHA'></th></span></q></dt></tr></i><div id='agJHA'><tfoot id='agJHA'></tfoot><dl id='agJHA'><fieldset id='agJHA'></fieldset></dl></div>

      1. <small id='agJHA'></small><noframes id='agJHA'>

      2. <legend id='agJHA'><style id='agJHA'><dir id='agJHA'><q id='agJHA'></q></dir></style></legend>

        获取对象字段以前的值休眠JPA

        时间:2023-09-27
          <bdo id='O84X7'></bdo><ul id='O84X7'></ul>

            1. <i id='O84X7'><tr id='O84X7'><dt id='O84X7'><q id='O84X7'><span id='O84X7'><b id='O84X7'><form id='O84X7'><ins id='O84X7'></ins><ul id='O84X7'></ul><sub id='O84X7'></sub></form><legend id='O84X7'></legend><bdo id='O84X7'><pre id='O84X7'><center id='O84X7'></center></pre></bdo></b><th id='O84X7'></th></span></q></dt></tr></i><div id='O84X7'><tfoot id='O84X7'></tfoot><dl id='O84X7'><fieldset id='O84X7'></fieldset></dl></div>

              • <tfoot id='O84X7'></tfoot>

                <small id='O84X7'></small><noframes id='O84X7'>

                  <tbody id='O84X7'></tbody>

                1. <legend id='O84X7'><style id='O84X7'><dir id='O84X7'><q id='O84X7'></q></dir></style></legend>
                  本文介绍了获取对象字段以前的值休眠JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设我有这个课程:

                  @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 类中,我试图找出以前的值 MyClass 实例与即将保存(更新)到数据库的新值.我使用更新前方法来做到这一点:

                  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 对象实例:

                  So assuming I have a MyClass objects instance with name and surname :

                  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 方法,当我尝试时:

                  So this triggers the preUpdate method in mylistener and when I try to :

                  MyClass.findById(someLongId);
                  

                  当我调试时,我已经获得了新更新的实例,但更新尚未发生,因为当我在 surname 列中检入数据库时​​,它仍然是 Dylan.

                  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?

                  推荐答案

                  我认为一种简单的方法是将先前的值保存在 JPA 不会持久的瞬态变量中.因此,只需引入一个变量 previousSurname 并保存实际值,然后再将其覆盖在 setter 中.

                  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.

                  如果你想保存多个属性,如果你的类 MyClassSerializable.

                  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 saveState(){
                          this.savedState = SerializationUtils.clone(this); // from apache commons-lang
                       }
                  
                  }
                  

                  但请注意,savedState 是一个分离的实例.

                  But be aware that the savedState is a detached instance.

                  然后您可以在 EntityListener 中访问之前的状态.

                  You can then access the previous state in your EntityListener.

                  您还可以将 PostLoad 侦听器移动到 EntityListener 类.但是您需要访问 savedState 字段.我建议将其设置为包作用域或使用包作用域访问器并将 MyClassMyListener 放在同一个包中,

                  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 saveState(MyClass myClass){
                           myClass.saveState(SerializationUtils.clone(myClass)); // from apache commons-lang
                      }
                  
                  }
                  
                  public class MyClass implements Serializable {
                  
                       @Transient
                       private transient MyClass savedState;
                  
                       void saveState(MyClass savedState){
                          this.savedState = savedState;
                       }
                  
                  }
                  

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

                  上一篇:休眠注释:实体没有默认构造函数 下一篇:有没有办法在 Java 中使用注释来替换访问器?

                  相关文章

                  最新文章

                  <tfoot id='r4vpD'></tfoot>

                    <small id='r4vpD'></small><noframes id='r4vpD'>

                      <bdo id='r4vpD'></bdo><ul id='r4vpD'></ul>
                  1. <legend id='r4vpD'><style id='r4vpD'><dir id='r4vpD'><q id='r4vpD'></q></dir></style></legend>
                    <i id='r4vpD'><tr id='r4vpD'><dt id='r4vpD'><q id='r4vpD'><span id='r4vpD'><b id='r4vpD'><form id='r4vpD'><ins id='r4vpD'></ins><ul id='r4vpD'></ul><sub id='r4vpD'></sub></form><legend id='r4vpD'></legend><bdo id='r4vpD'><pre id='r4vpD'><center id='r4vpD'></center></pre></bdo></b><th id='r4vpD'></th></span></q></dt></tr></i><div id='r4vpD'><tfoot id='r4vpD'></tfoot><dl id='r4vpD'><fieldset id='r4vpD'></fieldset></dl></div>