简体   繁体   English

休眠一对多注释外键null

[英]hibernate one to many annotation foreign key null

I have two entities LR.java and LI.java . 我有两个实体LR.javaLI.java One request may have more than one items on it. 一个请求可能包含多个项目。

After the execution of this method the lR is always null inside the item table. 执行此方法后,项目表中的lR始终为null。 What I am doing wrong here? 我在这里做错了什么? Any suggestions 有什么建议么

You defined a bidirectional "manytoone" association, so it's develepoer's responsability to keep consistent both sides of the association (that is: loanRequest and loanItems). 您定义了双向“ manytoone”关联,因此,开发者的责任是保持关联双方的一致性(即:loanRequest和loanItems)。

You should set the field loanRequest of the Loanitem entity to point to "right" LoanRequest. 您应该将Loanitem实体的字段loanRequest设置为指向“正确的” LoanRequest。 Do you so in the addLoanItem ? 您是否在addLoanItem中这样做?

Beside, unless you really need to "navigate" from one entity to the other and backward I suggest you to use unidirectional association, they are far more manageable and less prone to errors like this. 另外,除非您真的需要从一个实体“导航”到另一个实体,然后再向后导航,否则我建议您使用单向关联,它们将更易于管理,并且不易出现此类错误。

Anyway, @XtremeBaumer is right, constructor of LoanItem should be empty. 无论如何,@ XtremeBaumer是正确的,LoanItem的构造函数应该为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM