Hibernate OneToMany and ManyToOne confusion! Null List!
- by squizz
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.
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!
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!