<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!--

	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.typedmanytoone">

	<class name="Customer" 
			select-before-update="true" 
			dynamic-update="true">
	
		<id name="customerId">
			<generator class="assigned"/>
		</id>
	
		<property name="name" not-null="true"/>
		
		<many-to-one name="billingAddress" 
			entity-name="BillingAddress"
			cascade="persist,save-update,delete"
			fetch="join">
			<column name="billingAddressId"/>
			<formula>'BILLING'</formula>
		</many-to-one>
			
		<many-to-one name="shippingAddress" 
			entity-name="ShippingAddress"
			cascade="persist,save-update,delete"
			fetch="join">
			<column name="shippingAddressId"/>
			<formula>'SHIPPING'</formula>
		</many-to-one>
			
	</class>
	
	<class name="Address"
		table="Address"
		entity-name="BillingAddress" 
		where="add_type='BILLING'" 
		check="add_type in ('BILLING', 'SHIPPING')"
		select-before-update="true" 
		dynamic-update="true">
	
		<composite-id name="addressId"> 
			<key-property name="addressId"/>
			<key-property name="type" column="add_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"/>
						
	</class>

	<class name="Address" 
		table="Address"
		entity-name="ShippingAddress"
		where="add_type='SHIPPING'"
		select-before-update="true" 
		dynamic-update="true">
	
		<composite-id name="addressId">
			<key-property name="addressId"/>
			<key-property name="type" column="add_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"/>
						
	</class>

</hibernate-mapping>