-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Advanced Mapping
PostPosted: Tue Jun 21, 2005 4:30 am 
Newbie

Joined: Mon Jun 20, 2005 7:09 pm
Posts: 1
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 7:53 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello kennedyz,

I have the following proposal.

I think that you could omit the table ledger_lines. It has no special purpose as the ledger_table, which keeps track of a unique id across all types of ledgers (like bill, invoice, ...).
In this case the following would work.
If you do not even need a concrete table ledger you could omit it and use the "table of concrete class" strategy with union-subclass

Best Regards
Sebastian
Please rate, if this a solution.

Code:
<class name="Ledger" table="tledger" >
    <id name="id" >
      <generator class="sequence">
        <param name="sequence" >tledger_id_seq</param>
      </generator>
    </id>

  <joined-subclass name="Bill" table="tbill">
    <key column="ledger_id"></key>
    <property name="date" type="timestamp"></property>
    <list name="lines" table="tbill_line">
    <key column="parent_id"></key>
    <list-index column="id"></list-index>
      <composite-element class="BillLine">
        <property name="quantity" type="integer"></property>
        <property name="price" type="float"></property>
      </composite-element>
    </list>
  </joined-subclass>
  </class>

Code:
CREATE TABLE examples.tledger
(
  id int4 NOT NULL DEFAULT nextval('tledger_id_seq'::regclass),
  CONSTRAINT ledger_pkey PRIMARY KEY (id)
) ;

CREATE TABLE examples.tbill
(
  ledger_id int4 NOT NULL,
  date timestamp,
  CONSTRAINT tbill_pkey PRIMARY KEY (ledger_id)
) ;
CREATE TABLE examples.tbill_line
(
  id int4 NOT NULL DEFAULT nextval('tbill_line_id_seq'::regclass),
  parent_id int4 NOT NULL,
  quantity int4,
  price float8,
  CONSTRAINT tbill_line_pkey PRIMARY KEY (id, parent_id)
) ;

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.