An interesting thing I noticed while implementing this was that I had to put a ".trim()" in the setAddressType() in order to get the formula tag to work in the many-to-one mapping. (It was appending spaces to 'MAIL' and 'BILL' using Oracle.)
I also set not-found="ignore" on the billing address, since it is optional.
Anyway, here's the prototype which seems to be working:
Code:
package hibertest;
public class Person {
private String id;
private String firstName;
private String middleInitial;
private String lastName;
private USAddress mailingAddress;
private USAddress billingAddress;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMiddleInitial() {
return middleInitial;
}
public void setMiddleInitial(String middleInitial) {
this.middleInitial = middleInitial;
}
public USAddress getMailingAddress() {
return mailingAddress;
}
public void setMailingAddress(USAddress address) {
this.mailingAddress = address;
}
public USAddress getBillingAddress() {
return billingAddress;
}
public void setBillingAddress(USAddress address) {
this.billingAddress = address;
}
}
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibertest">
<class name="Person" table="MEMBER">
<id name="ID" column="MEMBER_ID"/>
<property name="firstName" column="FIRST_NAME"/>
<property name="middleInitial" column="MIDDLE_INITIAL"/>
<property name="lastName" column="LAST_NAME"/>
<many-to-one name="mailingAddress"
class="PersonUSAddress"
insert="false"
update="false">
<column name="MEMBER_ID"/>
<formula>'MAIL'</formula>
</many-to-one>
<many-to-one name="billingAddress"
class="PersonUSAddress"
insert="false"
update="false"
not-found="ignore">
<column name="MEMBER_ID"/>
<formula>'BILL'</formula>
</many-to-one>
</class>
</hibernate-mapping>
Code:
package hibertest;
public abstract class USAddress {
private String street1;
private String street2;
private String city;
private String state;
private String zip;
public String getCity() {
return city;
}
public String getStreet1() {
return street1;
}
public String getStreet2() {
return street2;
}
public String getState() {
return state;
}
public String getZip() {
return zip;
}
public void setCity(String city) {
this.city = city;
}
public void setState(String state) {
this.state = state;
}
public void setStreet1(String street1) {
this.street1 = street1;
}
public void setStreet2(String street2) {
this.street2 = street2;
}
public void setZip(String zip) {
this.zip = zip;
}
}
Code:
package hibertest;
import java.io.Serializable;
public class PersonUSAddress extends USAddress implements Serializable {
private String memberID;
private String addressType;
public String getAddressType() {
return addressType;
}
public void setAddressType(String addressType) {
this.addressType = addressType.trim();
}
public String getMemberID() {
return memberID;
}
public void setMemberID(String memberID) {
this.memberID = memberID;
}
}
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibertest">
<class name="PersonUSAddress" table="MEMBER_LOCATION">
<composite-id>
<key-property name="memberID" column="MEMBER_ID"/>
<key-property name="addressType" column="ADDRESS_TYPE"/>
</composite-id>
<property name="street1" column="ADDRESS_1"/>
<property name="street2" column="ADDRESS_2"/>
<property name="city" column="CITY"/>
<property name="state" column="STATE"/>
<property name="zip" column="ZIP"/>
</class>
</hibernate-mapping>
It's not even too ugly. Thanks!