Christophe
Christophe

Reputation: 21

Multiple discriminator columns in Hibernate inheritance hierarchy

I have a structure like this :

abstract class A
abstract class B extends A
abstract class C extends B

This is my actual mapping:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "TAB_A")
@DiscriminatorColumn(name = "DISCRIMINATOR_A",
                     discriminatorType = DiscriminatorType.STRING)
public abstract class A {
}

@Entity
@DiscriminatorValue("VALUE")
@SecondaryTable(name = "TAB_B",
                pkJoinColumns = @PrimaryKeyJoinColumn(name="ID_A"))
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DISCRIMINATOR_B",
                     discriminatorType = DiscriminatorType.STRING)
public abstract class B extends A
{
}

@Entity
@DiscriminatorValue("VALUEB")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class C extends B
{
}

I have a discriminator column between A and B that is discriminatorA.
I have a discriminator column between B and C that is discriminatorB.

The inheritance between A and B works. The inheritance between B and C doesn't work. I read this point:

11.1.10 DiscriminatorColumn Annotation

For the SINGLE_TABLE mapping strategy, and typically also for the JOINED strategy, the persistence provider will use a type discriminator column. The DiscriminatorColumn annotation is used to define the discriminator column for the SINGLE_TABLE and JOINED inheritance mapping strategies.

The strategy and the discriminator column are only specified in the root of an entity class hierarchy or subhierarchy in which a different inheritance strategy is applied.

Does anyone have an idea about how to make this mapping?

Upvotes: 2

Views: 1811

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42084

Remove this:

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

Having discriminator column once per hierarchy is enough. All entities in this hierarchy will have row in TAB_A (defined in entity A). This is also enough if you use joined inheritance strategy (multiple tables).

Other problems with your mappings:

  • A is the root of your entity hierarchy, using @Inheritance in B and C is not needed.
  • @DiscriminatorValue should be placed to concrete entities, B and C are abstract.

Upvotes: 1

Related Questions