在Hibernate 5.x中尝试加载记录时多次执行默认构造函数
有人能帮我理解一下,为什么在Hibernate5.x中加载记录时会多次执行默认构造函数 持久性类在Hibernate 5.x中尝试加载记录时多次执行默认构造函数,hibernate,Hibernate,有人能帮我理解一下,为什么在Hibernate5.x中加载记录时会多次执行默认构造函数 持久性类 public class Customer { private int cid; private String cphone; private String ccity; private String cemail; public Customer() { System.out.println("**Default Constructor*
public class Customer {
private int cid;
private String cphone;
private String ccity;
private String cemail;
public Customer() {
System.out.println("**Default Constructor**");
}
public Customer(String cphone, String ccity, String cemail) {
this.cphone = cphone;
this.ccity = ccity;
this.cemail = cemail;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCphone() {
return cphone;
}
public void setCphone(String cphone) {
this.cphone = cphone;
}
public String getCcity() {
return ccity;
}
public void setCcity(String ccity) {
this.ccity = ccity;
}
public String getCemail() {
return cemail;
}
public void setCemail(String cemail) {
this.cemail = cemail;
}
}
加载记录的步骤
日志
注意:第一次调用时使用SQLServer 2014和Hibernate 5.3.7进行了尝试
从Hibernate 5.3.7开始,第一个发生在会话工厂
初始化过程中:
if ( identifierGetter != null && constructor != null ) {
// use the id value of a newly instantiated instance as the unsaved-value
final Serializable defaultValue = (Serializable) identifierGetter.get( instantiate( constructor ) );
return new IdentifierValue( defaultValue );
}
似乎它可以计算出@Id
字段的哪个值指示@实体
未保存
看
第二个电话
您使用的是,它返回实体的代理(假设实体存在)
代理类是@Entity
所以代理类的构造函数调用超级构造函数。因此,第二次调用
第三个电话
然后强制Hibernate
初始化实体(调用getCemail()
)。
Hibernate
发出sql
语句,然后初始化。这是对构造函数的第三次调用
为了更好地理解正在发生的事情,我建议您调试它 在默认构造函数处放置断点并观察堆栈跟踪