[英]@OneToMany is giving null value in spring data jpa
public class Enterprise{
.....
@OneToMany(mappedBy = "enterprise", fetch = FetchType.EAGER)
private List<Organization> organizations;
.....
}
public class Organization{
....
@ManyToOne
@JoinTable(name = "enterprise_organization_map", joinColumns = {
@JoinColumn(name = "organization_id") }, inverseJoinColumns = { @JoinColumn(name = "enterprise_id") })
private Enterprise enterprise;
....
}
When I tried to fetch Enterprise, I am not getting the mapped organizations. 当我尝试获取Enterprise时,没有得到映射的组织。 its returning null. 返回的null。
//print statement //打印语句
Enterprise with Organizations:Enterprise [enterpriseId=13, enterpriseName=xyz, organizations=null] 具有组织的企业:企业[企业ID = 13,企业名称= xyz,组织=空]
Can anyone please help me here. 任何人都可以在这里帮助我。
Bidirectional one-to-many associations required helper methods to 'link' both entities, see the manual : 双向一对多关联需要帮助程序方法来“链接”两个实体,请参见手册 :
Whenever a bidirectional association is formed, the application developer must make sure both sides are in-sync at all times. 每当形成双向关联时,应用程序开发人员都必须确保双方始终保持同步。 The addPhone() and removePhone() are utilities methods that synchronize both ends whenever a child element is added or removed. addPhone()和removePhone()是实用程序方法,可在添加或删除子元素时同步两端。
So you have to add to Enterprise
at least one such method and use it when you add Organization
to Enterprise
: 因此,必须将至少一种此类方法添加到Enterprise
,然后在将Organization
添加到Enterprise
时使用它:
public Enterprise addOrganization(Organization organization) {
organization.setEnterprise(this);
this.organizations.add(organization);
return this;
}
I don't see such method in your code. 我在您的代码中看不到这种方法。 I think this is the reason of your problem. 我认为这是您遇到问题的原因。
Additional useful info about one-to-many you can find here: Best Practices for Many-To-One and One-To-Many Association Mappings . 有关一对多的其他有用信息,您可以在这里找到: 多对一和一对多关联映射的最佳做法 。
问题出在toString()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.