Clouds
Home

Hibernate 2.0 Introduction

Hibernate Mapping Declaration

Hibernate Development Code

Hibernate Generator

Inheritence Mapping

Hibernate Inheritance Mapping


Hibernate supports the three basic inheritance mapping strategies:
  • Table per Class Hierarchy
  • Table per Subclass
  • Table per Concrete Class

Table per class hierarchy

Suppose we have an interface Payment, with implementors CreditCardPayment, CashPayment, ChequePayment.
The table per hierarchy mapping would look like:

<class name="Payment" table="PAYMENT">
    <id name="id" type="long" column="PAYMENT_ID">
      <generator class="native"/>
    </id>
    <discriminator column="PAYMENT_TYPE" type="string"/>
      <property name="amount" column="AMOUNT"/>
      ...
    <subclass name="CreditCardPayment" discriminator-value="CREDIT">
    <property name="creditCardType" column="CCTYPE"/>
      ...
    </subclass>
    <subclass name="CashPayment" discriminator-value="CASH">
      ...
    </subclass>
    <subclass name="ChequePayment" discriminator-value="CHEQUE">
      ...
    </subclass>
</class>


Exactly one table is required. There is one big limitation of this mapping strategy:columns declared by the subclasses,such as CCTYPE, may not have NOT NULL constraints.

Table per subclass

Suppose we have an interface Payment, with implementors CreditCardPayment, CashPayment, ChequePayment.
The table per hierarchy mapping would look like:

<class name="Payment" table="PAYMENT">
    <id name="id" type="long" column="PAYMENT_ID">
      <generator class="native"/>
    </id>
    <discriminator column="PAYMENT_TYPE" type="string"/>
      <property name="amount" column="AMOUNT"/>
      ...
    <subclass name="CreditCardPayment" discriminator-value="CREDIT">
      <join table="CREDIT_PAYMENT">
        <property name="creditCardType" column="CCTYPE"/>
        ...
      </join>
    </subclass>
    <subclass name="CashPayment" discriminator-value="CASH">
      <join table="CASH_PAYMENT">
      ...
      </join>
    </subclass>
    <subclass name="ChequePayment" discriminator-value="CHEQUE">
      <join table="CHEQUE_PAYMENT" fetch="select">
      ...
      </join>
    </subclass>
</class>


Exactly one table is required. There is one big limitation of this mapping strategy:columns declared by the subclasses, such as CCTYPE, may not have NOT NULL constraints.

Table per subclass, using a discriminatorSuppose

<class name="Payment" table="PAYMENT">
    <id name="id" type="long" column="PAYMENT_ID">
      <generator class="native"/>
    </id>
    <discriminator column="PAYMENT_TYPE" type="string"/>
    <property name="amount" column="AMOUNT"/>
      ...
    <subclass name="CreditCardPayment" discriminator-value="CREDIT">
      <join table="CREDIT_PAYMENT">
        <property name="creditCardType" column="CCTYPE"/> ...
      </join>
    </subclass>
    <subclass name="CashPayment" discriminator-value="CASH">
      <join table="CASH_PAYMENT"> ... </join>
    </subclass>
    <subclass name="ChequePayment" discriminator-value="CHEQUE">
      <join table="CHEQUE_PAYMENT" fetch="select">
      ... </join>
    </subclass>
</class>


Table per concrete class, using implicit polymorphism An alternative approach is to make use of implicit polymorphism:

<class name="CreditCardPayment" table="CREDIT_PAYMENT">
    <id name="id" type="long" column="CREDIT_PAYMENT_ID">
    <generator class="native"/>
    </id>
    <property name="amount" column="CREDIT_AMOUNT"/>
    ...
</class>
<class name="CashPayment" table="CASH_PAYMENT">
    <id name="id" type="long" column="CASH_PAYMENT_ID">
    <generator class="native"/>
    </id>
    <property name="amount" column="CASH_AMOUNT"/>
    ...
    </class>
<class name="ChequePayment" table="CHEQUE_PAYMENT">
    <id name="id" type="long" column="CHEQUE_PAYMENT_ID">
    <generator class="native"/>
    </id>
    <property name="amount" column="CHEQUE_AMOUNT"/>
    ...
</class>

Disadvantages of hibernate

  • For complex data, mapping from Object-to-tables and vise versa reduces performance and increases time of conversion.
  • Hibernate does not allow some type of queries which are supported by JDBC. For example It does not allow to insert multiple objects (persistent data) to same table using single query. Developer has to write separate query to insert each object.


<<<< PREVIOUS: Hibernate Generator          NEXT: Hibernate Introduction >>>>


Home Clouds