-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate Many-to Many Question
PostPosted: Mon Jan 24, 2005 7:20 am 
Newbie

Joined: Wed Jan 05, 2005 5:30 am
Posts: 14
Hey

I am trying to create a Many to Many mapping. The rules goes like this.

A customer has many projects.
A project has many customers.

So customer and project each have a Set called projects and customers. How do I map this

I realise I have todo something like this.

for Project class mapping

<Many-to-many
column="FK_customersID"
class ="Customers"
/>

for Customers

<Many-to-many
column="FK_ProjectID"
class ="Project"
/>

But is this enough, I am missing something right... the SET declaration for each mapping???


Hibernate version:
2.1.7


Top
 Profile  
 
 Post subject: here's an example of how you would handle this many-to-many
PostPosted: Fri Jan 28, 2005 3:26 am 
Regular
Regular

Joined: Fri Jan 28, 2005 3:11 am
Posts: 81
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.