Hibernate version: 3.0.5
Mapping documents:
Code:
<class name="be.rmi.intranet.db.users.OrganizationalUnit" table="RH_OUNIT" lazy="false">
<id name="id">
<column name="OUNIT_ID" not-null="true" unique="true" sql-type="NUMBER"/>
<generator class="sequence">
<param name="sequence">RH_GENERIC_SEQ</param>
</generator>
</id>
<property name="key" column="OUNIT_KEY" type="string" not-null="false"/>
<property name="nameFr" column="OUNIT_NAME_FR" type="string" not-null="false"/>
<property name="nameNl" column="OUNIT_NAME_NL" type="string" not-null="false"/>
<set name="members" table="RH_EMP_OUNIT_LINK" cascade="all">
<key column="OUNIT_ID" not-null="true"/>
<many-to-many class="be.rmi.intranet.rh.Employee" column="EMP_ID"/>
</set>
</class>
Code:
public class OrganizationalUnit {
private long id;
private String nameFr;
private String nameNl;
private String key;
private Set members = new HashSet();
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
// ......
public Set getMembers() {
return members;
}
public void setMembers(Set members) {
this.members = members;
}
Code between sessionFactory.openSession() and session.close():Code:
//Get an "Employee", get and OrganziationUnit by id,
ounit.getMembers().add(newEmployee);
Transaction tx = session.beginTransaction();
session.update(ounit);
tx.commit();
//Note: using long running session in webapp environnment, session is associated with requesting user and reattached each time. That explain why you don't see the load() codes.
Name and version of the database you are using:Oracle 10
The generated SQL (show_sql=true):Code:
Hibernate: select organizati0_.OUNIT_ID as OUNIT1_, organizati0_.OUNIT_KEY as OUNIT2_37_, organizati0_.OUNIT_NAME_FR as OUNIT3_37_, organizati0_.OUNIT_NAME_NL as OUNIT4_37_ from RH_OUNIT organizati0_
Hello,
i have trouble persisting a many-to-many association. As you can see from mapping we use an association table, we then add() items to the association Set, but upon calling session.update() with the Hibernate object containing the Set, the Set is not persisted to the database.
As you can see from log, no update is done to take into account the add() call. However, debuggin with eclipse shows that ounit.getMembers() returns a PersistentSet, which mean hibernate should persist it's content during call to update (or not? at least it proves it handles the Set). Note that
1) replacing update with saveOrUpdate() has no more effect
2) calling update(newEmployee) does not have effect either
3) changing a property of ounit (like the nameFr), has the effect of ounit being updated to database, but not the association (nameFr is changed, association table remains untouched)
4) calling session.flush() has no effect either
[/code]