-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to map table per concrete class without discriminator?
PostPosted: Fri Jan 06, 2006 3:58 pm 
Beginner
Beginner

Joined: Sun Jun 05, 2005 9:45 am
Posts: 27
I have wasted a lot of time trying to get this mapping to work. Perhaps you can help:


Code:
CLASSES
--------------------------------------------------------------------------------
Order
    Collection payments
   
Payment (abstract class)
    Long paymentId
   
CashPayment extends Payment
    String someCashSpecificAttribute   
   
CheckPayment extends Payment
    String someCheckSpecificAttribute
   
CreditCardPayment extends Payment
    String someCreditCardpecificAttribute       


TABLES
--------------------------------------------------------------------------------
ORDER
    order_id
    some_attributes...
   
CASH_PAYMENT   
    cash_payment_id (PK)
    order_id (FK)
    some_cash_specific_attr
   
CHECK_PAYMENT   
    check_payment_id (PK)
    order_id (FK)
    some_check_specific_attr   
   
CREDIT_PAYMENT   
    credit_payment_id (PK)
    order_id (FK)
    some_credit_specific_attr 



I have tried mapping each class (CashPayment,CheckPayment and CreditCardPayment) as its own class (not seen here) and then joining them to the Order object using:
Code:
<class name="Order">
...       
    <set name="payments">
        <key>
            <column name="order_id" precision="22" scale="0" />
        </key>
        <one-to-many class="Payment" />
    </set>
...
</class>


Hibernate complains that the Payment class is not mapped. I thought this would work because it used implicit polymorphism. No Dice. Every other type of mapping requires some sort of discriminator. This is a "legacy" schema and I don't have the option to change it. There has got to be a way to map these tables as a collection.

Any ideas?

Regards,

Josh


Top
 Profile  
 
 Post subject: I should have also mentioned...
PostPosted: Sat Jan 07, 2006 9:19 am 
Beginner
Beginner

Joined: Sun Jun 05, 2005 9:45 am
Posts: 27
I am using Hibernate 3.x and Oracle 9.x


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 07, 2006 12:55 pm 
Newbie

Joined: Wed Dec 07, 2005 1:27 pm
Posts: 8
Yeah the problem is that table-per-concrete-class doesn't mix with polymorphic assoiations well. It's a little ugly but here's one way you could approach it... the Order class could have three collections: one of each of the concrete payment classes. This could be hidden from users of Order (by using private accessor methods), and exposed using public accessor methods that deal with all the payments together by encapsulating access to each of the sub-collections.

E.g.

public Collection getPayments() {
Set payments = new HashSet();
payments.addAll(cashPayments);
payments.addAll(checkPayments);
payments.addAll(creditCardPayments);
return payments
}

public void addPayment(Payment payment) {
if(payment instanceof CashPayment) {
cashPayments.add(payment);
} else if (payment instanceof CheckPayment) {
checkPayments.add(payment);
} else if (payment instanceof CreditCardPayment) {
creditCardPayments.add(payment);
}
}
}

public void setPayments(Collection payments) {
for(Payment payment : payments) {
addPayment(payment);
}
}


Top
 Profile  
 
 Post subject: thanks for the reply
PostPosted: Sat Jan 07, 2006 2:10 pm 
Beginner
Beginner

Joined: Sun Jun 05, 2005 9:45 am
Posts: 27
I thought about doing something like that. The only problem I see is this:

Collection payments = order.getPayments();
payments.add(someNewPayment);

I think this would produce a hard to find bug. The only solution I can think of is to return a unmodifiable collection to force people to use the order.addPayment() method.

Any other ideas?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.