Hibernate OneToMany和ManyToOne混淆!空列表!

时间:2021-03-05 15:20:48

I have two tables... For example - Company and Employee (let's keep this real simple)

我有两张桌子......例如 - 公司和员工(让我们保持这简单)

Company( id, name );
Employee( id,  company_id );

Employee.company_id is a foreign key.

Employee.company_id是外键。

My entity model looks like this...

我的实体模型看起来像这样......

Employee

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "company_id")
Company company;

Company

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "company_id")
List<Employee> employeeList = new ArrayList<Employee>();

So, yeah I want a list of employees for a company.

所以,是的,我想要一份公司员工名单。

When I do the following...

当我做以下事情......

Employee e = new Employee();
e.setCompany(c); //c is an Company that is already in the database.
DAO.insertEmployee(e); //this works fine!

If I then get my Company object it's list is empty!

如果我然后得到我的公司对象它的列表是空的!

Ive tried endless different ways from the Hibernate documentation!

我尝试过Hibernate文档中不同的方法!

Obviously not tried the correct one yet!

显然没有尝试过正确的!

I just want the list to be populated for me or find out a sensible alternative.

我只是希望为我填充列表或找到一个明智的选择。

Help would be greatly appreciated, thanks!

非常感谢帮助,谢谢!

2 个解决方案

#1


5  

You need to make the relationship bidirectional. Yours is two unidirectional ones. Add mappedBy = "company" to employeeList. This tells hibernate the employee list is simply the reverse of the ref from Employee to Company

您需要使关系双向。你的是两个单向的。将mappedBy =“company”添加到employeeList。这告诉hibernate员工列表只是从Employee到Company的ref的反向

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "company")
List<Employee> employeeList = new ArrayList<Employee>();

#2


0  

I dont think you should do List<Employee> employeeList = new ArrayList<Employee>(); This should be done when you have defined the side which has the list as the owning side which in your case is the Employee and not the Company. I think each time you load the Company instance, you are being supplied with a new instance of employeeList, not sure though. But surely, there is no need to instantiate the new ArrayList each time.

我不认为你应该做List employeeList = new ArrayList ();当您定义了将列表作为拥有方的一方(在您的情况下是员工而非公司)时,应该执行此操作。我想每次加载Company实例时,都会为您提供一个新的employeeList实例,但不确定。但可以肯定的是,每次都不需要实例化新的ArrayList。

#1


5  

You need to make the relationship bidirectional. Yours is two unidirectional ones. Add mappedBy = "company" to employeeList. This tells hibernate the employee list is simply the reverse of the ref from Employee to Company

您需要使关系双向。你的是两个单向的。将mappedBy =“company”添加到employeeList。这告诉hibernate员工列表只是从Employee到Company的ref的反向

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "company")
List<Employee> employeeList = new ArrayList<Employee>();

#2


0  

I dont think you should do List<Employee> employeeList = new ArrayList<Employee>(); This should be done when you have defined the side which has the list as the owning side which in your case is the Employee and not the Company. I think each time you load the Company instance, you are being supplied with a new instance of employeeList, not sure though. But surely, there is no need to instantiate the new ArrayList each time.

我不认为你应该做List employeeList = new ArrayList ();当您定义了将列表作为拥有方的一方(在您的情况下是员工而非公司)时,应该执行此操作。我想每次加载Company实例时,都会为您提供一个新的employeeList实例,但不确定。但可以肯定的是,每次都不需要实例化新的ArrayList。