简体   繁体   中英

Getting Callback after all setters called to add extra fields?

I'd like to override a setter so that I can perform some function on the data so I can return a calculated column for my entity. The function depends a several columns (eg COL1, COL2, ...) so I can't really intercept any particular setter because the other values might not yet be populated. Does hibernate provide some sort of "finish()" method that can be called once at the values are set for the Entity?

 @Override @Column(name="COL1") public String getCol1() { return this.col1; } @Override public void setCol1(String value) { super.setCol1(value); genMagicValue(); } public String getMagicValue() { return this.magicValue(); } 

除了休眠提供的功能之外,是否不能懒惰地初始化magicValue以便在第一次调用getMagicValue且随后对getMagicValue的后续调用仅返回计算值时进行计算?

I don't understand your question, setCol1 may be never called (and left with its default value). Furthermore, no one is preventing you to call it twice with different values.

perhaps the pattern you are looking for is:

  boolean magicDone=false;
  public String getMagicValue() {
       if (!magicDone){
           magicDone=true;
           genMagicValue();
       }
       return this.magicValue();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM