Hibernate OneToMany and ManyToOne confusion! Null List!
Posted
by
squizz
on Stack Overflow
See other posts from Stack Overflow
or by squizz
Published on 2011-01-26T15:49:37Z
Indexed on
2011/03/13
16:09 UTC
Read the original article
Hit count: 144
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!
© Stack Overflow or respective owner