Hi,
I am having a problem saving relationship between two POJOs.
The relationship is M:M and the POJO is the same type on both side of the relationship.
The POJO is Person and the relationship is M:M, one side is the child the other side is the parent.
The problem is the M:M table does not update. When I inspect the hibernate code, hibernate does not fire off SQL to update the M:M table.
Here is the POJO
public class Person extends JPAEntity {
private List <Person> children = new ArrayList();
private List <Person> parents = new ArrayList();
@ManyToMany
@JoinTable(name="RPERSONPERSONCHILD", joinColumns={@JoinColumn(name="C_FROM", referencedColumnName="C"),
@JoinColumn(name="I_FROM", referencedColumnName="I")},
inverseJoinColumns={@JoinColumn(name="C_TO", referencedColumnName="C"),
@JoinColumn(name="I_TO", referencedColumnName="I")})
// many to many rel
public List<Person> getChild() {
return children;
}
public void setChild(List<Person> children){
this.children = children;
}
@ManyToMany(mappedBy="child")
public List<Person> getParents(){
return parents;
}
public void setParents(List<Person> parents) {
this.parents = parents;
}
}
And here is the test code.
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Person person = new Person();
// Code to set the primary key
em.persist(person);
Person parentPerson = new Person();
// code to set the primary key
em.persist(parentPerson);
List <Person> list = new ArrayList();
list.add(parentPerson);
person.setParents(list);
List <Person> childList = new ArrayList();
parentPerson.setChild(childList);
em.persist(parentPerson);
em.persist(person);
em.getTransaction().commit();
// I check the Database now, the POJOs are persisted by the M:M relationship table is not updated.
|