gpt4 book ai didi

java - 区分从域调用的 setter 和从 hibernate 调用的 setter

转载 作者:行者123 更新时间:2023-12-01 12:11:23 26 4
gpt4 key购买 nike

当从域类调用它时,我想在我的 setter 中执行一些操作,而不是从 hibernate 中调用它时。此外,我正在使用 session 工厂,因此我无法使用 @PostLoad 来触发标志!

有人对此有任何想法吗?谢谢。

最佳答案

如果您使用注释并注释了字段,那么 Hibernate 将使用反射直接访问该字段,因此在 setter 中实现自定义逻辑应该没有问题。

如果您使用 XML 映射,则可以指定字段访问:

第 5.1.11 节 ( https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/mapping.html )

The access attribute allows you to control how Hibernate accesses the property at runtime. 
By default, Hibernate will call the property get/set pair. If you specify access="field",
Hibernate will bypass the get/set pair and access the field directly using reflection. Y
ou can specify your own strategy for property access by naming a class that
implements the interface org.hibernate.property.PropertyAccessor.

如果你想确定的话,比如:

private String name;

public void setName(String name){
if(this.name != null && ! this.name.equals(name){
//do something
}

this.name = name;
}

关于java - 区分从域调用的 setter 和从 hibernate 调用的 setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27257207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com