Matt Quinlan asked me how to map this relational model:

Customer (customerId, name)
Address(customerId, type, street, city, state, zip,
    primary key(customerId, type) )

To this object model:

Customer {customerId, name, billingAddress, shippingAddress}
Address {customerId, type, street, city, state, zip}

Where the billingAddress and shippingAddress are really two
different one-to-one associations, both mapped to the
customerId pk/fk, and distinguished by the value of the
type column.

After some hacking about and various different experiments,
I hit upon some small changes I could make in order to support
this mapping:

<!--

   Shows how to map a one-to-many relationship in the relational
   schema to "typed" one-to-one associations in the object model.
   We map the Address class twice, with different entity names,
   specifying a filtering condition in each mapping. The typed
   associations then reference the named entities.

-->

<hibernate-mapping package="org.hibernate.test.typedonetoone">

<class name="Customer">
<id name="customerId">
<generator class="assigned"/>
</id>
<property name="name" not-null="true"/>
<one-to-one name="billingAddress"
entity-name="BillingAddress"
property-ref="customer"
cascade="create"
fetch="join"/>
<one-to-one name="shippingAddress"
entity-name="ShippingAddress"
property-ref="customer"
cascade="create"
fetch="join"/>
</class>
<class name="Address"
table="Address"
entity-name="BillingAddress"
where="type='BILLING'"
check="type in ('BILLING', 'SHIPPING')">
<composite-id name="addressId">
<key-property name="customerId"/>
<key-property name="type"/>
</composite-id>
<property name="street" not-null="true"/>
<property name="city" not-null="true"/>
<property name="state" not-null="true"/>
<property name="zip" not-null="true"/>
<many-to-one name="customer"
column="customerId"
insert="false"
update="false"/>
</class>


<class name="Address"
table="Address"
entity-name="ShippingAddress"
where="type='SHIPPING'">
<composite-id name="addressId">
<key-property name="customerId"/>
<key-property name="type"/>
</composite-id>
<property name="street" not-null="true"/>
<property name="city" not-null="true"/>
<property name="state" not-null="true"/>
<property name="zip" not-null="true"/>
<many-to-one name="customer"
column="customerId"
insert="false"
update="false"/>
</class>


</hibernate-mapping>


-- Gavin King +61 410 534 454 +1 404 822 8349 callto://gavinking

Hibernate
[EMAIL PROTECTED]
http://hibernate.org

JBoss Inc
[EMAIL PROTECTED]
http://jboss.com



-------------------------------------------------------
The SF.Net email is sponsored by: Beat the post-holiday blues
Get a FREE limited edition SourceForge.net t-shirt from ThinkGeek.
It's fun and FREE -- well, almost....http://www.thinkgeek.com/sfshirt
_______________________________________________
hibernate-devel mailing list
hibernate-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to