[英]Finding all private fields and their corresponding getters / setters for nested classes
[英]Getting Callback after all setters called to add extra fields?
我想重写一个setter,以便可以对数据执行某些功能,以便为我的实体返回一个计算列。 该函数取决于几列(例如,COL1,COL2等),因此我无法真正拦截任何特定的设置器,因为可能尚未填充其他值。 休眠是否提供某种“ finish()”方法,可以在为实体设置值时调用一次?
@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的后续调用仅返回计算值时进行计算?
我不明白您的问题,可能永远不会调用setCol1(并保留其默认值)。 此外,没有人阻止您使用不同的值两次调用它。
也许您正在寻找的模式是:
boolean magicDone=false;
public String getMagicValue() {
if (!magicDone){
magicDone=true;
genMagicValue();
}
return this.magicValue();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.