Here's how I did a similar one many-to-many deal, except change group to project, and user to customer. I hope this will help you.
Firstly, here's my hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- properties -->
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/workflow</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">blah</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mapping files -->
<mapping resource="com/talisen/workflow/hibernate/User.hbm.xml"/>
<mapping resource="com/talisen/workflow/hibernate/Group.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Then here's my User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.talisen.workflow.hibernate">
<class name="User" table="user">
<id name="userId" column="USER_ID" type="java.lang.Integer">
<generator class="increment"/>
</id>
<property name="userName" column="USER_NAME" type="java.lang.String" not-null="true" />
<set name="groups" table="user_group" inverse="true">
<key column="user_id" />
<many-to-many column="group_id"
class="com.talisen.workflow.hibernate.Group" />
</set>
</class>
</hibernate-mapping>
And here's my Group.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.talisen.workflow.hibernate">
<class name="Group" table="groupyo">
<id name="groupId" column="GROUP_ID" type="java.lang.Integer">
<generator class="increment" />
</id>
<property name="groupName" column="GROUP_NAME"
type="java.lang.String" not-null="true" />
<property name="groupDesc" column="GROUP_DESC"
type="java.lang.String" not-null="true" />
<set name="users" table="user_group">
<key column="group_id" />
<many-to-many column="user_id"
class="com.talisen.workflow.hibernate.User" />
</set>
</class>
</hibernate-mapping>
There's no need to actually create a separate Java class or hibernate mapping for the user_group database table that you would use to track your user/group association (or customer/project) association.
Note: the user one has an inverse="true" attribute, which means that the group is the one that will manage this releationship.
|