I am trying to convert an existing application to hibernate for persistence so I want to initially map between the existing value objects and tables without having to refactor any java code.
I am having trouble with one concept in the value objects that is not represented in the reference guide clearly.
Many of the value objects have composite entities that are stored in different tables but those tables do not have a foreign key back to the containing object and nor should they as they don't necessarily refer always to the same class.
An example might make it clearer. There is an Address class and and address table. Many other classes have an address_id which indicate which row to load into the value object (by a kind of builder pattern in the data access logic). This way anything that has an address (company, client, user) has just an id and the builder fills in the Address object before returning results to the client. It does a similar operation in reverse when saving an object with an Address object inside it.
I want hibernate to fill in this composite address on load and update it in another table on save but not sure what to put in the XML so that takes place.. Suggestions appreciated..
public class Account
implements java.io.Serializable
{
// persisted
private int id;
private String firstName;
private String lastName;
private String description;
private String insurance_company;
private String insurance_policyNumber;
private Date insurance_expiryDate;
private String insurance_contactName;
private String insurance_contactPhoneNumber;
private boolean insurance_letterIndemnityReceived;
private String typeId;
private String company;
private int addressId; // <--- This indicates what to load into Address
// dynamic
private Address address;
private Driver[] drivers;
...
}
public class Address
implements java.io.Serializable {
private int id;
private String street1;
private String street2;
private String city;
private String county;
private String postCode;
private String phone1;
private String phone2;
...
}
|