I have a many to many relationship between groups and users that I have mapped as shown at the bottom of this post. I now would like to add an additional column to the user_group database, which would need to be reflected in the set element that I use in both group and user hibernate-mappings to keep track of their many-to-many relationship as is shown below. Unlike the user_id and group_id, the third column is not part of the primary key. This column happens to be a status_id column which is a foreign key to the status table which contains status names such as "active"/ "inactive." (it's keyed by status_id, of course). So in the the context of my user/group relationship, this additional column would provide me with a way of signifying that a given user is no longer in a given group without having to delete a row from the user_group table. How would I map that third column in hibernate?
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>
|