-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problem saving M:M rel
PostPosted: Tue Feb 20, 2007 12:08 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
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.


Top
 Profile  
 
 Post subject: answer
PostPosted: Tue Feb 20, 2007 1:01 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
Sorry simple problem.
The fix is to add the following line:

childList.add(child)

It's interesting that is left up to the app to wire the relationship on both sides, why is this?
Surely it is part of the O/R and it should do it?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.