I'm building a data services layer for a multitier application. A DAO model will be used to map between domain objects and the database and hibernate is the engine.
I can't figure out how to get hibernate to retrieve the keys instead of the actual referenced objects. Perhaps if I want to return only references to the caller in the other tier I need to copy the objects into DTOs to map the references not actual referencees?
For example, here's a simple slice of the data model:
Code:
public class Customer {
public CustomerKey key;
public CustomerFamilyKey cfKey;
public String name;
}
public class CustomerKey {
public long id;
public int version;
}
and customers have a many-to-one relationship with a CustomerFamily:
Code:
public class CustomerFamily {
public CustomerFamilyKey key;
public String name;
}
public class CustomerFamilyKey {
public long id;
public int version;
}
Note that both objects are versioned and have composite keys. Not sure if that matters.
In the object that I return to the calling tier (caller -> StatelessSessionBean -> DAO -> RDBMS) I am intending to use a style of interface that provides on the the primary key of the parent/family not a reference to the actual object:
Code:
public class Customer {
public CustomerFamilyKey key;
.
.
Hibernate seems to want to make this more like:
Code:
public class Customer {
public CustomerFamily family;
.
.
is there even a way to declare that what I want is the list of associated ids instead of the associated objects themselves?
Or, do I need to have the Hibernate managed objects give me back the connected objects and then I need to copy things over into more referenced-by-id DTOs?
Thanks in advance.
Here are the snippets of .hbm code I'm using if that helps:
Code:
<hibernate-mapping package="foo">
<class name="Customer" table="CUSTOMER">
<composite-id name="primaryKey" class="Customer" unsaved-value="any">
<key-property name="id" column="id"/>
<key-property name="version" column="version"/>
</composite-id>
<property name="name"/>
<many-to-one name="family" class="CustomerFamilyVO" cascade="all">
<column name="family_id"/>
<column name="family_version"/>
</many-to-one>
</class>
</hibernate-mapping>
and
Code:
<hibernate-mapping pacakge="foo">
<class name="CustomerFamily" table="CUSTOMER_FAMILY">
<composite-id name="primaryKey" unsaved-value="any">
<key-property name="id" column="id"/>
<key-property name="version" column="version"/>
</composite-id>
<property name="name"/>
</class>
</hibernate-mapping>