aspose file tools*
The moose likes Object Relational Mapping and the fly likes JPA - understanding OneToMany bidirectional relations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA - understanding OneToMany bidirectional relations" Watch "JPA - understanding OneToMany bidirectional relations" New topic
Author

JPA - understanding OneToMany bidirectional relations

Nuno Feliciano
Greenhorn

Joined: Sep 10, 2010
Posts: 4
Hi,

I think I don't understand very well how the OneToMany bidirectional relations work in JPA.
In the database I have the following tables/records
Professor(ID INTEGER,DEPARTMENT_ID INTEGER)

Select * from Professor
ID |DEPARTMENT_ID
-----------------------
1 |1

Department(ID INTEGER)

Select * from Department
ID
---
1


My Entities are:

@Entity
public class Department {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@OneToMany(mappedBy="department")
private Collection<Professor> employees;

public Department() {
employees = new ArrayList<Professor>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Collection<Professor> getProfessors() {
return employees;
}
}


@Entity
public class Professor {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@ManyToOne
private Department department;

public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}



When I execute
Query selectDepartment= em.createQuery("SELECT d from Department d");
Department department = (Department)selectDepartment.getSingleResult();
Collection<Professor> professors = department.getProfessors();

I get an empty collection

I was expecting to get a collection with 1 element. What am I missing here?


Thank you if you reached this far.

Nuno
Karsten Wutzke
Ranch Hand

Joined: Jul 20, 2010
Posts: 106

Your forward association



is missing @JoinColumn or @JoinColumns. The mapped-by counterpart is ok though.

Karsten


OCJP JavaSE 6 (86%)
Nuno Feliciano
Greenhorn

Joined: Sep 10, 2010
Posts: 4
It doesn´t make any difference
I get the same result.
Nuno Feliciano
Greenhorn

Joined: Sep 10, 2010
Posts: 4
By the way...
First I created the Entities and then generated the tables in the DB with JPA. And I didn't make any changes in the Entities or the DB afterwords.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
My guess is you have caching enabled and when you create the objects you did not set the Many side of the relationship.

i.e.
when you create the Department of the Professor you need to set the reference back
see,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side

Or you could disable caching, or refresh the object. To confirm the issue try calling refresh() on the Department.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Nuno Feliciano
Greenhorn

Joined: Sep 10, 2010
Posts: 4
Thanks for the link, James.

I realised I had to change the setDepartment method in the Professor Entity.
public void setDepartment(Department department) {
this.department = department;
if (!department.getProfessors().contains(this)) {
department.getProfessors().add(this);

}
}
I realized I have to "explicitly" add the professor to the department.
I thought JPA would do that automatically, but I guess I was wrong.

Thanks again.

Nuno
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA - understanding OneToMany bidirectional relations