gpt4 book ai didi

java - Hibernate 调用错误的 setter

转载 作者:行者123 更新时间:2023-11-30 08:11:37 24 4
gpt4 key购买 nike

我对 hibernate 从数据库加载一些 bean 的方式有一些问题。

我们使用 bean/beanHistoric 结构来保存对该 bean 所做的所有更改。当我们在 bean 实例中保留一些更改时,我们创建一个具有相同数据的 beanHistoric 并保存它,以便某些 setter 不完全是 setter 。例如:

@Entity
public class beanHistoric {
List<AnotherBeanHistoric> anotherBeanListH;

@OneToMany(mappedBy="beanHistoric", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
public List<AnotherBeanHistoic> getAnotherBeanList(){
return this.anotherBeanListH;
}
public void setAnotherBeanList(List<AnotherBean> anotherBeanList){
for (AnotherBean anotherBean : anotherBeanList){
anotherBeanListH.add(new AnotherBeanHistoric(anotherBean))
}
}

private void setAnotherBeanListH(List<AnotherBeanHistoric> anotherBeanList){
this.anotherBeanListH = anotherBeanList;
}
}

如您所见,该属性写为 anotherBeanListH,但 hibernate 调用 setAnotherBeanList 来填充对象,一旦它从数据库加载而不是 setAnotherBeanListH。

知道为什么会这样吗?

最佳答案

您通过注释 getter getAnotherBeanList() 告诉 Hibernate 名为 anotherBeanList 的属性被映射为 OneToMany。因此,当从数据库中读取实体时,它会通过调用关联的 setter 来填充关联:setAnotherBeanList()。反之则令人惊讶。如果属性名为 foo,为什么 Hibernate 会调用 setBar()

关于java - Hibernate 调用错误的 setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30884850/

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