Java Reference
In-Depth Information
a good way to persist interfaces, but because Java lacks support for multiple inher-
itance there are situations where it conflicts with existing uses of inheritance.
Luckily, the
Coupon
hierarchy is extremely simple and so we do not have this
problem. To persist these classes, we would define an
AbstractCouponImpl
class
that implements the
Coupon
interface. The concrete coupon classes such as
Free-
ShippingCoupon
extend this class. Here is part of the
O/R
mapping document for
the
Coupon
class hierarchy:
<class name="AbstractCouponImpl"
table="COUPON">
…
<discriminator column="COUPON_TYPE" />
<subclass
name="FreeShippingCoupon"
discriminator-value="FREE_SHIP">
<property name="code" column="CODE" />
<property name="minimum" column="MINIMUM" />
</subclass>
…
</class>
This mapping specifies that the
AbstractCouponImpl
class is mapped to the
COU-
PON
table and the
<discriminator>
element specifies that the discriminator col-
umn, which stores the type of the coupon, is called
COUPON_TYPE
. The
<subclass>
element specifies that the
FreeShippingCoupon
subclass of
Coupon
is mapped to
the same table. The
discriminator-value
attribute of the
<subclass>
element
specifies that the discriminator value for the
FreeShippingCoupon
is
FREE_SHIP
.
The
<property>
elements map the fields of the
FreeShippingCoupon
class to the
COUPON
table.
The
<many-to-one>
for the
PendingOrder.coupon
field has the
class="Abstract-
CouponImpl"
attribute:
<class name="PendingOrder" table="PENDING_ORDER">
…
<many-to-one name="coupon"
class="AbstractCouponImpl" column="COUPON_ID"
/>
…
</class>
This attribute specifies that the
PendingOrder.coupon
field is really a reference to
an
AbstractCouponImpl
even though its type is
Coupon
. This approach eliminates
the need to add accessors for the identifier property to the
Coupon
and improves
encapsulation.
Search WWH ::
Custom Search