Java Reference
In-Depth Information
to a table in the same way as a class. You must instead change the class hierarchy by
introducing a common superclass that implements the interface and then map it
to the database table.
For example, to persist the
Coupon
hierarchy we define an
AbstractCoupon-
Impl
class that implements the
Coupon
interface. The concrete
Coupon
classes
such as
FreeShippingCoupon
and
PercentageDiscountCoupon
extend this class:
interface Coupon {..};
class AbstractCouponImpl implements Coupon {…};
class FreeShippingCoupon extends AbstractCouponImpl {…};
class PercentageDiscountCoupon extends AbstractCouponImpl {…};
Once we have made these changes to the domain model, we can then define the
XML
metadata for the
O/R
mapping for the
Coupon
class hierarchy. Here is an
excerpt of the
XML
metadata that defines the mapping for the
AbstractCoupon-
Impl
and
FreeShippingCoupon
classes:
<class name="AbstractCouponImpl" table="COUPON" >
<implements name="net.chrisrichardson.foodToGo.domain.Coupon"/>
<inheritance strategy="new-table">
bbbb
<discriminator strategy="value-map">
bbb
<column name="COUPON_TYPE"/>
bbbb
</discriminator>
bbbb
</inheritance>
</class>
<class name="FreeShippingCoupon" persistence-capable-superclass=
➥
"net.chrisrichardson.foodToGo.domain.AbstractCouponImpl">
<inheritance strategy="superclass-table">
<discriminator value="FREE_SHIP"/>
</inheritance>
<field name="minimum">
<column name="FREE_SHIP_MINIMUM" allows-null="true"/>
</field>
</class>
The
<implements>
element specifies that the
AbstractCouponImpl
class imple-
ments the
Coupon
interface. This tells the
JDO
implementation that any field of
type
Coupon
is really a reference to an instance of this class or one of its subclasses.
The
<inheritance>
element of the
AbstractCouponImpl
specifies that the
AbstractCouponImpl
class has its own table called
COUPON
and that the
COUPON_TYPE
column stores the type of the coupon. The
<inheritance>
ele-
ment of the
FreeShippingCoupon
class specifies that the
FreeShippingCoupon
is mapped to the
COUPON
table and that its type code is
FREE_SHIP
. The map-
ping for
PercentageDiscountCoupon
would be defined in a similar way.
Search WWH ::
Custom Search