Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA - understanding OneToMany bidirectional relations

 
Nuno Feliciano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Hibernate MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your forward association



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

Karsten
 
Nuno Feliciano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn´t make any difference
I get the same result.
 
Nuno Feliciano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nuno Feliciano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic