-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping question
PostPosted: Thu Nov 24, 2005 2:14 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 3:07 am
Posts: 33
Location: Sydney
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3

I have 2 tables in my database (MySQL) - event and vendor. An event can have many vendors assigned to it and conversely, a vendor can be assigned to many events. So, from my perspective there is 2 one-to-many relationships.

My plan is to have an association table, say, event_vendors like so:

CREATE TABLE event_vendors
( event_id BIGINT NOT NULL,
vendor_id BIGINT NOT NULL,
PRIMARY KEY( event_id, vendor_id ),
FOREIGN KEY (event_id) REFERENCES event(event_id),
FOREIGN KEY (vendor_id) REFERENCES vendor(vendor_id)
) ENGINE=InnoDB;

And my mapping docs, as follows:

Mapping documents:

<hibernate-mapping>

<class name="com.wikidsistas.model.Vendor"
table="vendor"
optimistic-lock="version">
<cache usage="read-write"/>
<id name="vendorId" column="vendor_id" type="java.lang.Long" unsaved-value="null">
<generator class="increment"/>
</id>
<version name="version" column="version" unsaved-value="null"/>
<property name="name" type="string" column="vendor_name"/>
<property name="contact" type="string" column="contact_name"/>
<property name="emailAddress" type="string" column="email_address"/>
<property name="phone" type="string" column="phone"/>
<component name="address" class="com.wikidsistas.model.Address">
<property name="addressLine1" type="string" column="address_line1"/>
<property name="addressLine2" type="string" column="address_line2"/>
<property name="suburb" type="string" column="suburb"/>
<property name="state" type="string" column="state"/>
<property name="postcode" type="integer" column="postcode"/>
</component>
<component name="vendorType" class="com.wikidsistas.model.VendorType">
<property name="vendorTypeId" type="long" column="vendor_type_id"/>
<property name="vendorType" type="string" column="vendor_type_name"/>
</component>
<set name="events" table="event_vendors"inverse="true">
<key column="vendor_id"/>
<one-to-many class="com.wikidsistas.model.Event"/>
</set>
</class>

</hibernate-mapping>



<hibernate-mapping>

<class name="com.wikidsistas.model.Event"
table="event"
optimistic-lock="version">
<cache usage="read-write"/>
<id name="eventId" column="event_id" type="java.lang.Long" unsaved-value="null">
<generator class="increment"/>
</id>
<version name="version" column="version" unsaved-value="null"/>
<property name="name" type="string" column="event_name"/>
<property name="description" type="string" column="event_description"/>
<property name="venue" type="string" column="venue"/>
<property name="dateMillis" type="long" column="event_date_millis"/>
<property name="availableTickets" type="int" column="tickets_available"/>
<property name="priceRules" type="string" column="ticket_price_rules"/>
<component name="address" class="com.wikidsistas.model.Address">
<property name="addressLine1" type="string" column="address_line1"/>
<property name="addressLine2" type="string" column="address_line2"/>
<property name="suburb" type="string" column="suburb"/>
<property name="state" type="string" column="state"/>
</component>
<set name="vendors" table="event_vendors" inverse="true">
<key column="event_id"/>
<one-to-many class="com.wikidsistas.model.Vendor"/>
</set>
</class>
</hibernate-mapping>


Each class will have methods to manipulate the sets.

Is this the best way to map this relationship?

I've ready many articles which lead me to different conclusions.

Thanks,
C.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 7:54 am 
Newbie

Joined: Thu Sep 01, 2005 8:43 am
Posts: 14
many-to-many? Or (some say) it is better to have your own association class that has a one-to-many to both of your other classes.
cu,


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 7:57 am 
Beginner
Beginner

Joined: Mon Apr 12, 2004 6:33 pm
Posts: 35
I think you should use "many-to-many" instead of "one-to-many". Other than that your mapping looks fine to me.

_________________
Kees de Kooter

(don't hesitate to rate ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 8:55 am 
Beginner
Beginner

Joined: Sun Oct 16, 2005 3:07 am
Posts: 33
Location: Sydney
Yeah, the Hibernate best practices recommends a 2 one to many relationship and an association class to tie the 2. I'm still not sure though. I'll give both a try and see what works best, though in my mind the relationship each way is really a one-to-many and I'm inclined to go down that route.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.