2 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以使用 Hibernate@NotFound注釋:
@ManyToOne
@NotFound(action=NotFoundAction.IGNORE)
private City birthCity;
https://docs.jboss.org/hibernate/orm/5.3/javadocs/index.html?org/hibernate/annotations/NotFound.html
我沒(méi)有看到其他已發(fā)布的解決方案工作,因?yàn)樵?Hibernate 加載時(shí)會(huì)發(fā)生異常,即在您能夠通過(guò)其他方式處理它之前。

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
我假設(shè)你有
Person{
@Many2One @JoinColumn("birthCityId") City birthCity;
...}
最簡(jiǎn)單的解決方案是在 city 中添加 id=0 的表行,其余為空
這給你的班級(jí)
@PostLoad
public viod cityInit(){
if(birthCity!=null&&birthCity.getId()==0){
birthCity==null;
}
}
有更優(yōu)雅的解決方案,但這將使您快速入門
添加回答
舉報(bào)