Unfortunately, in the current releases of Hibernate what you would have to do to acheive this is define subclasses of your Pointer class for each type of entity it can reference and then map those subclasses. That would look something like:
Code:
<class name="Account" table="Account" >
...
<many-to-one name="customer" column="cust_id" class="CustomerPointer" not-null="true" />
<many-to-one name="agent" column="agent_id" class="AgentPointer" not-null="true" />
</class>
<class name="CustomerPointer" table="Customer">
<id name="id" column="cust_id">
<generator class="native"/>
</id>
<property name="name" column="cust_name"/>
</class>
<class name="AgentPointer" table="Agent">
<id name="id" column="agent_id">
<generator class="native"/>
</id>
<property name="name" column="agent_name"/>
</class>
Assuming you would also have independent mappings for Customer and Agent, this is essentially the "light weight" pattern described in the Hibernate patterns section of the site. Check that out for limitations to this approach.
In general, Hibernate works better if you utiize a (for lack of a better term) normalized domain model. So what if you did something like this instead:
Code:
class Account {
private String number;
private double balance;
private Customer customerRef;
private Customer jointCustomerRef;
private Agent agent;
// normal get/set pairs
...
// accessors for UI Pointers...
public Pointer getCustomer() {
if ( getCustomerRef() == null ) {
return null;
}
else {
return new Pointer( getCustomerRef().getId(), getCustomerRef().getName() );
}
}
...
}
Or even, better have a base class used by all your domain entities which forces subclasses to define a getPointer() method. That way you'd be able to centralize the way a Pointer looks for a given entity. Then the getCustomer() pointer accessor method above could just be:
Code:
public Pointer getCustomer() {
return getCustomerRef() == null
? null
: getCustomerRef().getPointer();
}