2

I understood how to use DiscriminatorColumn in Hibernate inheritance mapping, However, in my scenario, I have more complicated inheritance module, where I need to define two Discriminators. I want to use one table for the entire inheritance tree. Basically, the parent class is abastract, and it represents a person entity, then, I have two different abstract implementation of it, Wife & Husband. so basically I already need one discriminator. then for each (Wife & Husband) I have different messages that needs to be persisted, so I need no to create a discriminator for each (Wife & Husband).

I came up with this implementation, but I'm stuck, how do I continue from here ?

@Entity
@Table (name="sex")
@Inheritance (strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn (name="transaction_type", discriminatorType=DiscriminatorType.STRING)
public abstract class Person {
...
}

@Entity
@DiscriminatorValue("wife")
public class Wife extends Person {
...
}

now, how do I put a discriminatorcolumn on the Wife Class, the same as I did with the Parent ?

stdcall
  • 27,613
  • 18
  • 81
  • 125
  • Why do you need 'second discriminator'? Is this different message an indicator of another subclass of Person? If so - can't you create another subclasses? If not, than can you use just a regular field and some boolean value to indicate what kind of 'message' will be held in it? – Piotr Nowicki Dec 02 '11 at 23:48
  • @PiotrNowicki, each message is a different class, it includes different fields, and of course, the messages are discriminated by the type of the Person, Wife has it own messages, and husband has it's own. – stdcall Dec 03 '11 at 11:43
  • So `Wife` and `Husband` entities cannot have, respectively, `WifeMessage` and `HusbandMessage` as a fields? – Piotr Nowicki Dec 03 '11 at 11:55
  • Ok, the basic concept is like this, I want to have a table which is called "pending-messages". when a person logs to the system it looks if he have messages, this is a generic behavior to both with & husband. now he want's to retrieve the message from the table, so the're supposed to be a discriminator column for that also. – stdcall Dec 03 '11 at 11:59

1 Answers1

2

You don't need the seccond discriminator. the Wife Entity will work correctly.
In 2-level inheritance situations, the middle classes (wife and husband in this case) does not nead to have @DiscriminatorColumn.
the related question Problem with 2 levels of inheritance may be helpful.
the other question Hibernate Inheritance Single_table

Community
  • 1
  • 1
Dariush Jafari
  • 5,223
  • 7
  • 42
  • 71