I've two tables... For instance - Company and Worker (let us bare this real simple)
Company( id, name ); Employee( id, company_id );
Worker.company_id is really a foreign key.
My entity model appears like this...
@ManyToOne(cascade = CascadeType.PERSIST) @JoinColumn(name = "company_id") Company company;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "company_id") List<Employee> employeeList = new ArrayList<Employee>();
So, yeah I would like a listing of employees for an organization.
After I perform 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!
Basically then get my opportunity object it's list is empty!
Ive attempted endless various ways in the Hibernate documentation!
Clearly not attempted the most appropriate one yet!
Among the finest their email list to become populated for me personally or discover a smart alternative.
Help could be greatly appreciated, thanks!
You have to result in the relationship bidirectional. Yours is 2 unidirectional ones. Add
mappedBy = "company" to
employeeList. This informs hibernate the worker list is just the reverse from the ref from
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "company") List<Employee> employeeList = new ArrayList<Employee>();
I dont think you want to do
List<Employee> employeeList = new ArrayList<Employee>();
Do this if you have defined along side it that has their email list because the possessing side which inside your situation may be the Worker and never the organization. I believe every time you load the organization instance, you're being provided with a brand new demonstration of employeeList, unsure though. But surely, there's you don't need to instantiate the brand new ArrayList every time.