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.