hibernateclassreferenceconcrete

Hibernate: table per concrete class mapping & referencing


I am referring to table per concrete class as explained here: Table per concrete class

Following the example there in 9.1.6, let's say I have CashPayment, CreditCardPayment, and the superclass of both is an abstract Payment class.

If I am using a "table per concrete class" type of mapping, I will have two tables: CASH_PAYMENT and CREDIT_PAYMENT, and no PAYMENT table. That is all fine.

However, how can I build another entity type called Xyz which contains a reference/property to a Payment (either cash or CC)...? From a Java perspective, it's okay: Xyz.pamentType, but how can I configured this in HBM/annotations, and what would the XYZ table look like?

It might have a PAYMENT_ID column, but obviously it cannot be a foreign key as there's two potential tables to refer to. I'm guessing XYZ would need some type of additional column to say which type of Payment it is.

Or will Hibernate join both tables, using an ID, to find the correct type each time I read an instance of Xyz?

By the way, I know this is a bad approach, and I can use much simpler alternatives, but I've simplified the question as the answer (rather than an alternative) will be useful for me. Thanks!


Solution

  • There is quite much nothing to configure, just creating relationship to Payment, for example:

    @OneToOne private Payment payment;
    

    and then assigning value to it and persisting them.

    No additional columns are used to find out type of the Payment. To determine type of the Payment Hibernate