[
http://opensource.atlassian.com/projects/hibernate/browse/HBX-1039?page=c...
]
Max Rydahl Andersen resolved HBX-1039.
--------------------------------------
Resolution: Incomplete
you are only updating the inverse side, not the other side. you need to put elements in
there before things are kept in sync.
this is expected normal hibernate behavior.
many-to-many associations not working with hibernate
----------------------------------------------------
Key: HBX-1039
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HBX-1039
Project: Hibernate Tools
Issue Type: Bug
Environment: Hibernate Tools 3.2 beta 10, Oracle 10G,
Reporter: Vijaya Krishnan
Associated tables in a Many-To-Many relationship not updating with Hibernate. No HQL is
generated to insert values into the link table. Using Hibernate Tools 3.2 beta 10
Here are the hbm and java snippets.
-------------------------------------------------------------------------------------
Person.hbm.xml
<!-- bi-directional many-to-many association to Staff -->
<set name="staffs" table="PERSON_STAFF" lazy="false"
inverse="true" cascade="save-update">
<key>
<column name="PERSON_OID" precision="22"
scale="0" not-null="true" />
</key>
<many-to-many class="domain.Staff" column="STAFF_OID"
fetch="join" />
</set>
</class>
-----------------------------
Staff.hbm.xml
<!-- bi-directional many-to-many association to Person -->
<set name="persons" table="PERSON_STAFF"
cascade="all">
<key>
<column name="STAFF_OID" precision="22"
scale="0" not-null="true" />
</key>
<many-to-many column="PERSON_OID"
class="domain.Person" />
</set>
--------------------------------------------------------------------
person.java
public class Person implements java.io.Serializable {
private long oid;
private String userid;
private String firstname;
private String lastname;
private Set staffs = new HashSet();
//other getters and setters here.
public Set getStaffs() {
return this.staffs;
}
public void setStaffs(Set staffs) {
this.staffs = staffs;
}
------------------------------------------------------
staff.java
public class Staff implements java.io.Serializable {
private long oid;
private String staffname;
private long headcount;
private Set persons = new HashSet(0);
//other getters and setters here.
public Set getPersons() {
return persons;
}
public void setPersons(Set persons) {
this.persons = persons;
}
}
------------------------------------------------------------------------
Link table columns:
CREATE TABLE PERSON_STAFF( PERSON_OID NUMBER NOT NULL, STAFF_OID NUMBER NOT NULL)
PERSON table has primary key oid and STAFF table has primary key oid as well. The link
table is made of only the columns - person-oid and staff_oid. The CONSTRAINTs of priamry
and foreign key relationships are all set in the database.
-------------------------------------------------------------------
The lines of code to update the tables:
person.getStaffs().add(newStaff); //Line 1
update(person); //Line 2
-------------------------------------------
A person is associated with multiple staffs e.g. five staffs. The line 2 update generates
five updates to the staff table instead of one insert in the link table.
The above does not update the link table PERSON_STAFF, but updates the person and staff
table.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira