Hi,
I have searched the forums to a similar solution to this problem but have had no luck.
I have an existing schema that defines tables as follows:
Tables
Table 1: ledger_tx
----------------------
ledgerid int8 (primary key)
Table 2: invoice
----------------------
invoiceid (references ledger_tx.ledgerid)
Table 3: ledger_lines
-------------------
ledgerid (references ledger_tx.ledgerid)
seq_num
primary key (ledger_id, seq_num)
Table 4: invoice_lines
-------------------------
invoiceid (references invoice.invoice_id)
seq_num
unit_price
unit_qty
line_price
primary key (invoice_id,seq_num)
Classes
C1. BasicLedger
C2. BasicLedger.Line
C3. BasicInvoice (extends BasicLedger)
C4. BasicInvoice.Line (extends BasicLedger.Line )
C5. BasicPrice
Mappings:
This mapping describes what I am trying to acheive, but it doesn't work because BasicPrice is defined as a component of BasicInvoice.Line which is defined as a composite-element. I want to be able to save everything with a single call to
session.save(BasicInvoice).
Code:
<class name="BasicLedger" table="ledger">
<id name="id" unsaved-value="null" />
<property name="ledgerid" insert="false" update="false" />
<list name="ledgerLines" table="ledger_lines">
<key not-null="true">
<column name="ledgerid"/>
</key>
<list-index column="seq_num" base="0" />
<composite-element class="BasicLedger.Line">
</composite-element>
</list>
<!-- BasicInvoice extends BasicLedger -->
<joined-subclass name="BasicInvoice" table="invoice">
<key not-null="true">
<column name="invoiceid" />
</key>
<list name="invoiceLines" table="invoice_lines">
<key>
<column name="invoiceid"/>
</key>
<list-index column="seq_num" base="0" />
<!--BasicInvoice.Line extends BasicLedger.Line-->
<composite-element class="BasicInvoice.Line">
<component name="Price"
class="BasicPrice">
<property name="unitPrice" not=null="true" />
<property name="unitQty" not-null="true"/>
<property name="linePrice" not-null="true" />
</component>
</composite-element>
</list>
</joined-subclass>
</class>
To work around the BasicPrice component problem, I went re-read the docs and I tried to use a nested-composite-element, but that caused another error saying that a nested-composite-element isn't allowed in a list.
The documentation suggested defining BasicInvoice.Line as an entity, and use a one-to-many relation. I don't like this idea for a few reasons:
1. I need to change my BasicInvoice.ItemLine class to support an ID class
2. I need to account for the list sequence number value in my business logic.
3. I need to manually assign the invoice id to the item.
...
Is there another way I can map the BasicInvoice.ItemList objects so that a call to session.save(BasicInvoice) will save everything in one shot?