[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

hibernate many-to-many relationship question

Asked by aturetsky in Java Programming Language

Tags: hibernate, many, relationship


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>
Accepted Solution

Your question has an Asker Certified™ answer! aturetsky verified that this solution worked for them--which means it will likely work for you, too. Click to view the solution free for 30-days now.

About this solution

Zone: Java Programming Language
Tags: hibernate, many, relationship
Sign Up Now!
Solution Provided By: drjustin
Participating Experts: 2
Solution Grade: A
 
 
Related Solutions
Keywords: hibernate many-to-many relationship…
 
Loading Advertisement...
 
 
Loading Advertisement...
20100412-EE-VQP-154