views:

409

answers:

1

I am dealing with the following scenario:

We use table-per-subclass inheritance, meaning the concrete tables' primary keys are foreign key references on the abstract table. Superclass is Product, subclasses are Book, DVD, AudioCD, ...

Now in the Java superclass, say Product.java, we have an enum of the types of products: book, dvd, music, etc.

We have no discriminator column in the abstract table and no extra table for the types.

Is it possible to map the type enum in Product.java to the correct values, depending on the concrete object? Or is a discriminator or an extra table necessary?

... ....

Table per subclass inheritance mapping excerpt:

 <class name="Product" table="PRODUCT">
    <id name="id" column="IDPRODUCT" type="int">
        <generator class="native" />
    </id>
    ...

    <joined-subclass name="Book" table="BOOK">
        <key column="IDPRODUCT" />

        <property ...

    </joined-subclass>
 ...

... ...

Product.java

public class Product {

    public enum Type { book, dvd, music }

    ...

    private Type type;

    ...

Sounds odd? Maybe. Two separate parties designed the OO part and the DB part ...

+3  A: 

I may be missing something, but why would you even have Type as a field? Make getType() abstract in Product and implement it in subclasses to return appropriate value.

ChssPly76