Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Object Relational Mapping and the fly likes Hibernate @DiscriminatorColumn Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate @DiscriminatorColumn" Watch "Hibernate @DiscriminatorColumn" New topic

Hibernate @DiscriminatorColumn

Tom Bigbee

Joined: Sep 16, 2002
Posts: 19
I have four classes that map to four tables

ClassType, MainClass, SubClassOne, SubClassTwo

ClassType maps to the table "Class_Type" and consists of two attributes
1) Class_Type_Cd C(4) Not Null PK
2) Class_Type_Desc VC(40) Not Null

MainClass maps to the table "Main_Class" and consists of three attributes
1) Main_Class_ID Integer Not Null Auto_Generated PK
2) Class_Type_Cd (FK from Class_Type)
3) Main_Class_Name VC(40) Not Null

SubClassOne maps into the table "Sub_Class_One" and consists of two attributes
1) Sub_Class_One_ID Integer PK (Dependent upon Main_Class_ID)
1) Int_Value Integer Not Null

SubClassTwo is the same as SubClassOne, with a String_Value (as opposed to an Int_Value)

I would like to join the MainClass with either SubClassOne or SubClassTwo, based upon the value of the ClassTypeCd (fk into the MainClass)

I am using annotations, and cannot for the life of me figure out how to use the key value as opposed to a set string value, I would like to be able to set the below...

@DiscriminatorColumn(name="???", discriminatorType=DiscriminatorType.STRING)

where "??? is the value of this.getClassType().getClassTypeCd()

that way I can use the ClassType Description to get at the subclass from the MainClass

Does anyone have an example of how to do this?

Thanks, in advance, Thomas

PS - this is not a homework question, I'm trying to come up with a template to effectively add subtype tables to a schema without going into my main (superclass/supertype) constructs

Thomas Bigbee - SCJP, SCJD, SCWCD
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
The discriminator column is meant to be used in the "single table per class hierarchy" mapping. Doesn't apply here...
Tom Bigbee

Joined: Sep 16, 2002
Posts: 19
Thanks, however, that still does not answer my question

The book - Java Persistence with Hibernate lists four types of inheritance Paradigms

#4 Table per subclass�Represent is a (inheritance) relationships as has a (foreign key) relationships.

Some database systems (Oracle, for example) limit the number of
tables in an outer join operation. For a wide hierarchy, you may want to switch to a different fetching strategy that executes an immediate second select instead of an outer join:

I'm wondering if anybody has an example of this with annotations
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
In the sample, the thing is called "discriminator-value", and there is, in JPA at least, a @DiscriminatorValue annotation. Maybe this is what you are looking for...
I agree. Here's the link:
subject: Hibernate @DiscriminatorColumn