Hi all,
I am working with a legacy database that has a relational model something like this:
Code:
table orders
column id int primary key
... {etc}
table charge_lines
column order_id int } Combined primary key
column line_no int }
... {etc}
table product_lines
column order_id int }
column charge_line_no int } Combined primary key
column product_line_no int }
I have tried modeling this using composite-elements, but unfortunately hibernate does not allow composite-elements to contain collections themselves. So the only way to model this is treating rows in each table as an entity. This is possible by modeling charge lines and product lines using composite ids.
The object model looks something like this:
Code:
class Order
int id;
List chargeLines;
class ChargeLine
ChargeLineID chargeLineID;
List productLines;
class ChargeLineID
int orderId;
int lineNo;
class ProductLine
ProductLineID productLineID;
class ProductLineID
int orderId;
int chargeLineNo;
int productLineNo;
But here's the rub. An order can be rated or re-rated, during the process of which it will delete some charge lines, retain some, and auto generate others. Since the key of each charge line is dependant on its line number index, new charge lines can possess keys that were previously assigned to deleted lines. And existing charge lines can actually change their order in the parent list, which essentially changes their id in the database.
This causes a problem during updates of previously persisted orders. I am finding that the session complains that the newly created lines already exist, and have been deleted.
Is there any way around this problem (eg. in Hibernate3.0), or is a better mapping called for? The mapping document I am using follows:
Hibernate version: 2.18
Mapping documents:Code:
<hibernate-mapping>
<class name="Order" table="orders" lazy="true">
<id name="id" column="id" type="integer">
<generator class="assigned"/>
</id>
<list name="chargeLines" table="charge_lines" lazy="true" cascade="all-delete-orphan">
<key column="order_id"/>
<index column="line_no" type="integer" />
<one-to-many class="ChargeLine" />
</list>
</class>
<class name="ChargeLine" table="charge_lines" lazy="true">
<composite-id name="chargeLineID" class="ChargeLine$ChargeLineID">
<key-property name="orderId" column="order_id" type="integer"/>
<key-property name="lineNo" column="line_no" type="integer"/>
</composite-id>
<list name="productLines" table="product_lines" lazy="true" cascade="all-delete-orphan">
<key>
<column name="order_id"/>
<column name="charge_line_no"/>
</key>
<index column="product_line_no" type="integer" />
<one-to-many class="ProductLine" />
</list>
</class>
<class name="ProductLine" table="product_lines" lazy="true">
<composite-id name="productLineID" class="ProductLine$ProductLineID">
<key-property name="orderId" column="order_id" type="integer"/>
<key-property name="chargeLineNo" column="charge_line_no" type="int"/>
<key-property name="productLineNo" column="product_line_no" type="int"/>
</composite-id>
</class>
</hibernate-mapping>
regards,
Scott