File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Using @DiscriminatorColumn for Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Using @DiscriminatorColumn for Inheritance" Watch "Using @DiscriminatorColumn for Inheritance" New topic

Using @DiscriminatorColumn for Inheritance

Heathcliff Della-Franca

Joined: Jan 19, 2004
Posts: 2
Hi, I am wondering if anyone can help.

I have a set of classes Apple, Orange and Banana which all inherit from the Fruit class. I want to use the Single Table Strategy to persist all of the Fruit classes.

I understand I can use a @DiscriminatorColumn('FruitType') in the Fruit class and the @DiscriminatorValue('Apple'), @DiscriminatorValue('Orange') and @DiscriminatorValue('Banana') in the Apple, Orange and Banana classes, then make sure I have the FruitType column on the database set to either Apple, Orange or Banana.

What I would rather do if possible is have a FruitTypes table in the database to hold a list of valid @DiscriminatorValues, something like this:

Id Name
1 Apple
2 Orange
3 Banana

Then I could set up FruitTypeId on the Fruit table as a foreign key to FruitTypes and instead of using 'Apple', 'Orange' or 'Banana' I could use 1, 2 or 3.

However I would still like to keep the meaningful @DiscriminatorValues in the Apple, Orange and Banana classes i.e. @DiscriminatorValue('Apple') instead of @DiscriminatorValue('1').

Is there a value for @DiscriminatorColumn that will achieve this? Something like:

@DiscriminatorColumn('SELECT FruitType.Name FROM FruitType, Fruit WHERE FruitType.Id = Fruit.FruitTypeId')
I agree. Here's the link:
subject: Using @DiscriminatorColumn for Inheritance
jQuery in Action, 3rd edition