*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Using @DiscriminatorColumn for Inheritance" Watch "Using @DiscriminatorColumn for Inheritance" New topic
Author

Using @DiscriminatorColumn for Inheritance

Heathcliff Della-Franca
Greenhorn

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')
 
wood burning stoves
 
subject: Using @DiscriminatorColumn for Inheritance
 
Similar Threads
Need advice on a simple design
Generic class declarations
Please Help: Putting Vector objects into HashMap
The 'DateFormat' abstract class
cast int to an enum