Many times we need only one readonly field from a joined table. e.g, in following case I need only (customer)name from Customers table.For this I am creating entire Customer object, that is consisting of tens of other properties that I don't need in Orders.
This issue becomes more significant if such field is being fetched for child class e.g., (product)name for item object. This is leading to creation of hundreds or even thousands of "product" objects for one order just to provide product name.
One way to deal with such situation could be to make a additional CustomerName class with only one property. This approach does'nt look elegant because it could end up creating many similar classes for same table.
Can someone suggest me a better solution?
Example Mapping with usual Customer and Product classes:
<class name="Order" table="Orders">
<id column="ID" name="id" type="long">
<generator class="native" />
</id>
<property name="customerId" type="int" />
<many-to-one class="Customer" name="customer" column="customerId" insert="false" update="false" fetch="join" />
<class name="OrderItem" table="OrderItems">
<id column="id" name="itemId" type="long">
<generator class="native" />
</id>
<property name="productId" type="long" />
<many-to-one name="order" column="orderId" not-null="true" />
<many-to-one class="Product" name="product" column="productId" insert="false" update="false" fetch="join" />
</class>
</class>
Example with additional CustomerName and ProductName classes:
<class name="Order" table="Orders">
...
<many-to-one class="CustomerName" name="customer" column="customerId" insert="false" update="false" fetch="join" />
<class name="OrderItem" table="OrderItems">
.....
<many-to-one class="ProductName" name="product" column="productId" insert="false" update="false" fetch="join" />
</class>
</class>