Hi!
I have a small problem with many-to-many association.
Situation: I have a User and Grp object and the many-to-many link is between these two objects.
Platform: Tomcat 5.0.18, JDK 1.4.2, Hibernate 2.1.3, Mysql 4.0.17 and Mysql J/Connection 3.0
Problem: I create an User object and I add Grp object to User (Grp is read from database), then I save the User.
Result: User object is stored to database but table that is used for many-to-many relation (usr_group) remains empty.
Mapping file:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping auto-import="true"
package="com.tahonen.domain">
<class name="User" table="usr">
<id name="id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="username" column="usr_nam"/>
<property name="password" column="pwd"/>
<property name="email" column="email"/>
<property name="phone" column="phone"/>
<property name="location" column="loc"/>
<property name="birthYear" column="birth_year"/>
<property name="description" type="text"/>
<set name="groups" table="usr_group" lazy="true" cascade="all">
<key>
<column name="usr_id" not-null="true"/>
</key>
<many-to-many class="Grp">
<column name="grp_id" not-null="true"/>
</many-to-many>
</set>
</class>
<class name="Grp" table="grp">
<id name="id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="name"/>
<property name="created"/>
<many-to-one name="owner" class="User" column="owner"/>
</class>
</hibernate-mapping>
Java code:
Code:
. . .
Session s = HibernateUtil.currentSession();
User usr = new User();
usr.setUsername("username");
usr.setPassword("password");
usr.setCreated(new Date());
Set groups = new HashSet();
Grp g = ....//read grp from database
//I have tested that variable g is != null
groups.add(g);
usr.setGroups(groups);
s.save(usr);
I have also tested, that if Grp object that is added to User is not in db it's insterted to correct place, but still usr_group table remains empty
thanks,
tero