hibernate-in-action-completebook

Page 123

100

CHAPTER 3

Mapping persistent classes

Listing 3.8

Hibernate <subclass> mapping

B

<hibernate-mapping> Root class, mapped to table <class name="BillingDetails" table="BILLING_DETAILS" discriminator-value="BD"> <id name="id" column="BILLING_DETAILS_ID" type="long"> <generator class="native"/> </id>

C Discriminator column

<discriminator column="BILLING_DETAILS_TYPE" type="string"/>

D Property mappings

<property name="name" column="OWNER" type="string"/> ...

E CreditCard subclass

<subclass name="CreditCard" discriminator-value="CC"> <property name="type" column="CREDIT_CARD_TYPE"/> ... </subclass> ... </class> </hibernate-mapping>

B

The root class BusinessDetails of the inheritance hierarchy is mapped to the table BUSINESS_DETAILS.

C

We have to use a special column to distinguish between persistent classes: the discriminator. This isn’t a property of the persistent class; it’s used internally by Hibernate. The column name is BILLING_DETAILS_TYPE, and the values will be strings— in this case, "CC" or "BA". Hibernate will automatically set and retrieve the discriminator values.

D

Properties of the superclass are mapped as always, with a <property> element.

Licensed to Jose Carlos Romero Figueroa <jose.romero@galicia.seresco.es>


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.