Java Vibes..

June 22, 2009

Hibernate Inheritance

Hibernate supports different types of inheritance, here we explain it with the help of some examples. Please consider the following structures for the purpose of the examples:

Tables:

payment_t: pmt_id (PK), amount, paid_by, date_time

cash_payment_t: pmt_id (PK), currency

credit_payment_t: pmt_id (PK), cc_no, cc_type

cheque_payment_t: pmt_id (PK), cheque_no

Value objects:

Interfaces:

PaymentVo

Classes:

CashPaymentVo

CreditCardPaymentVo

ChequePaymentVo

Hibernate supports four types of inheritance mapping:

1. Table per class hierarchy:

This can be used if cash_payment_t etc are not separate tables, instead there is a pmt_type (discriminator) column in the payment_t tables that distinguishes the type of payment and all other columns are also part of payment_t

The limitation here is that child-specific columns like cc_type will have to be not-null in the common table.

<class name=”PaymentVo” table=”payment_t”>

<id name=”pmtId” type=”long” column=”pmt_id”>

<generator/>

</id>

<discriminator column=”pmt_type” type=”string”/>

<property name=”amount” column=”amount”/>

<subclass name=”CreditCardPaymentVo” discriminator-value=”CREDIT”>

<property name=”ccType” column=”cc_type”/>

</subclass>

<subclass name=”CashPaymentVo” discriminator-value=”CASH”>

</subclass>

<subclass name=”ChequePaymentVo” discriminator-value=”CHEQUE”>

</subclass>

</class>

2. Table per subclass hierarchy:

<class name=”PaymentVo” table=”payment_t”>

<id name=”pmtId” type=”long” column=”pmt_id”>

<generator/>

</id>

<property name=”amount” column=”amount”/>

<joined-subclass name=”CreditCardPaymentVo” table=”credit_payment_t”>

<key column=”pmt_id”/>

<property name=”ccType” column=”cc_type”/>

</joined-subclass>

<joined-subclass name=”CashPaymentVo” table=”cash_payment_t”>

<key column=”pmt_id”/>

</joined-subclass>

<joined-subclass name=”ChequePaymentVo” table=”cheque_payment_t”>

<key column=”pmt_id”/>

</joined-subclass>

</class>

or if there is a discriminator column in the payment table then:

<class name=”PaymentVo” table=”payment_t”>

<id name=”pmtId” type=”long” column=”pmt_id”>

<generator/>

</id>

<discriminator column=”pmt_type” type=”string”/>

<property name=”amount” column=”amount”/>

<subclass name=”CreditCardPaymentVo” discriminator-value=”CREDIT”>

<join table=”credit_payment_t”>

<key column=”pmt_id”/>

<property name=”ccType” column=”cc_type”/>

</join>

</subclass>

<subclass name=”CashPayment” discriminator-value=”CASH”>

<join table=”cash_payment_t”>

<key column=”pmt_id”/>

</join>

</subclass>

<subclass name=”ChequePayment” discriminator-value=”CHEQUE”>

<join table=”cheque_payment_t” fetch=”select”>

<key column=”pmt_id”/>

</join>

</subclass>

</class>

3. Table per concrete class hierarchy:

This is used if each of the child tables define the common columns themselves (i.e. amount is a column in each of the child tables)

Here the parent table payment_t may not be required at all, and if it is not there then the ParentVo can be defined as abstract=”true”

<class name=”PaymentVo”>

<id name=”pmtId” type=”long” column=”pmt_id”>

<generator/>

</id>

<property name=”amount” column=”amount”/>

<union-subclass name=”CreditCardPaymentVo” table=”credit_payment_t”>

<property name=”ccType” column=”cc_type”/>

</union-subclass>

<union-subclass name=”CashPaymentVo” table=”cash_payment_t”>

</union-subclass>

<union-subclass name=”ChequePaymentVo” table=”cheque_payment_t”>

</union-subclass>

</class>

4. Implicit polymorphism hierarchy (with table per concrete class):

Here no need to mention the PaymentVo interface in the mapping file

This one is not recommended as it has some limitations…

<class name=”CreditCardPaymentVo” table=”credit_payment_t”>

<id name=”pmtId” type=”long” column=”pmt_id”>

<generator/>

</id>

<property name=”amount” column=”amount”/>

</class>

<class name=”CashPayment” table=”cash_payment_t”>

<id name=”pmtId” type=”long” column=”pmt_id”>

<generator/>

</id>

<property name=”amount” column=”amount”/>

</class>

<class name=”ChequePayment” table=”cheque_payment_t”>

<id name=”pmtId” type=”long” column=”pmt_id”>

<generator/>

</id>

<property name=”amount” column=”amount”/>

</class>

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: