-->
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: Subclassing & oracle sequences
PostPosted: Thu Jul 21, 2011 12:05 pm 
Newbie

Joined: Thu Jul 21, 2011 11:41 am
Posts: 5
I have a pre-existing data structure in Oracle where one table is the "parent" which will join to one of a handful of "child" tables depending on what type that parent is. The sample provided in http://docs.jboss.org/hibernate/core/3. ... tance.html is a pretty close match, where PAYMENT would be a parent table, which may join to tables (for example) CREDITCARDPAYMENT, CASHPAYMENT.

So what I'd like to do if possible is have one base class called Payment containing some common info that is in the PAYMENT table and four subclasses of that for each of the other tables.

The snag I am hitting on that I haven't found an answer for is each of these child tables has its own primary key and an associated Oracle sequence.

FYI: I can't use annotations since I'm generating the POJO classes from an XSD.


Code:
CREATE TABLE PAYMENT (
  PAYMENTID INT, -- PRIMARY KEY
  PAYMENTTYPE VARCHAR(10), -- will be 'CC' or 'CASH', discriminator
  ...
);

CREATE TABLE CREDITCARDPAYMENT (
  CCPAYMENTID INT, -- PRIMARY KEY
  CCPARENTPAYMENTID INT, -- FK TO PAYMENT.PAYMENTID
  CCNUM VARCHAR(16),
  ...
);

CREATE TABLE CASHPAYMENT (
  CASHPAYMENTID INT, -- PRIMARY KEY
  CASHPARENTPAYMENTID INT, -- FK TO PAYMENT.PAYMENTID
  ...
);


each of the above has its own sequence.

Code:
   <class name="Payment" table="PAYMENT">
       <id name="paymentId" type="java.math.BigInteger">
         <column name="PAYMENTID" precision="22" scale="0"/> 
            <generator class="sequence"> 
                <param name="sequence">PAYMENTID_SEQ</param>               
            </generator>              
       </id>
       
       <discriminator column="PAYMENTTYPE" type="string"/>
      ...      
       <subclass name="CreditCardPayment" discriminator-value="CC">
          <id name="creditCardPaymentId" type="java.math.BigInteger">
            <column name="CCPAYMENTID" precision="22" scale="0"/> 
               <generator class="sequence"> 
                   <param name="sequence">CREDITCARDPAYMENT_SEQ</param>               
               </generator>              
           </id>
         <property column="CCNUM" name="ccNum" type="java.lang.String" />
      
           <join table="CREDITCARDPAYMENT">
               <key column="CCPARENTPAYMENTID"/>
           </join>
       </subclass>      
       <subclass name="CashPayment" discriminator-value="CASH">
          <id name="cashPaymentId" type="java.math.BigInteger">
            <column name="CASHPAYMENTID" precision="22" scale="0"/> 
               <generator class="sequence"> 
                   <param name="sequence">CASHPAYMENT_SEQ</param>               
               </generator>              
           </id>
         <property column="... "/>
      
           <join table="CASHPAYMENT">
               <key column="CASHPARENTPAYMENTID"/>
           </join>
       </subclass>      
   </class>



the above doesn't work because the subclass doesn't allow the <id /> block. How can I define a separate ID for subclasses? I think maybe the polymorphic approach using an <any /> block might work but it's unclear to me how I'd join each separate subclass from the sample given.


Top
 Profile  
 
 Post subject: Re: Subclassing & oracle sequences
PostPosted: Thu Jul 21, 2011 12:24 pm 
Newbie

Joined: Thu Jul 21, 2011 11:41 am
Posts: 5
To expand a bit on what I hope to achieve (and maybe determining that it's not possible), suppose I have data like this:

Code:
SELECT * FROM PAYMENT :

PAYMENTID   PAYMENTTYPE
---------   -----------
1           CC
2           CASH


SELECT * FROM CREDITCARDPAYMENT

CCPAYMENTID   CCPAYMENTPARENTID
-----------   -----------
10            1


SELECT * FROM CASHPAYMENT

CASHPAYMENTID   CASHPAYMENTPARENTID
-------------   -----------
20              2


of course there'd be more data than this but the above gives you the relational data.. a select with left joins might look like:

Code:
SELECT PAYMENTID, PAYMENTTYPE, .. FROM PAYMENT
    LEFT JOIN CREDITCARDPAYMENT ON PAYMENTID = CCPARENTPAYMENTID
    LEFT JOIN CASHPAYMENT ON PAYMENTID = CASHPARENTPAYMENTID


PAYMENT record where PAYMENTID=1 would join/subclass to CREDITCARDPAYMENT where CCPARENTPAYMENTID = 1..

So within Hibernate when saving a new credit card payment record, I'd like to create a CreditCardPayment object (which extends from Payment), set the values, and when it saves have it populate PAYMENT.PAYMENTID using PAYMENT_SEQ and CREDITCARDPAYMENT.CCPAYMENTID using CREDITCARDPAYMENT_SEQ

When loading, using the above data, session.load(Payment.class, 1) would return a CreditCardPayment (by virtue of that record having 'CC' in the discriminator column and joining to CREDITCARDPAYMENT table) using a where clause like "payment.paymentId = 1". session.load(CreditCardPayment.class, 10) would load the SAME record, using a where clause like "creditCardPayment.ccPaymentId = 1".

Is this arrangement even possible?


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.