20

Is it possible to force hibernate to use discriminator column for inheritance type joined? According to JPA2.0 specification this should be possible but i can't achieve it in hibernate.

Example:

@Inheritance(strategy = InheritanceType.JOINED)
@ForceDiscriminator
@DiscriminatorColumn(name="TYPE")
@Entity
public class Parent

@Entity
@DiscriminatorValue("C")
public class Child extends Parent

This doesn't even create column TYPE in the table PARENT when using hibernate.hbm2ddl.auto create.

I know that InheritanceType.JOINED works without defining discriminator column but it's quite ineffective because then hibernate needs to create joins between parent and all children instead of just parent and one child when using information in discriminator column.

milbr
  • 1,015
  • 1
  • 10
  • 16
  • 1
    After further search looks like it's not supporter by Hibernate annotations: http://opensource.atlassian.com/projects/hibernate/browse/ANN-140 – milbr Nov 25 '10 at 10:13

2 Answers2

15

I've used SINGLE_TABLE with a Discriminator and a SecondaryTable on the subclass to do this very thing. I.E.

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="TYPE")
@Entity
public class Parent

@Entity
@DiscriminatorValue("C")
@SecondaryTable(name = "child", pkJoinColumns = {@PrimaryKeyJoinColumn(name="id", referencedColumnName = "id")})
public class Child extends Parent

When you add a new sub class you add a new table with the relevant extended data fields in it.

Ivar
  • 6,138
  • 12
  • 49
  • 61
BadgerB
  • 306
  • 3
  • 7
  • 4
    This does not seem irrelevant to me. – jhericks May 27 '12 at 22:02
  • @DiscriminatorValue("C") in this attribute do we can put primitive type or object type because having this C as a value it makes it more rigid can't be it object type but it is not working – ThinkTank Mar 14 '19 at 07:14
1

Do you want to use @Inheritance(strategy=InheritanceType.SINGLE_TABLE)?

Jinesh Parekh
  • 2,131
  • 14
  • 18
  • No I don't, I would like to have normalized schema in database. With InheritanceType.SINGLE_TABLE when you add some child you'll have to change structure of parent table (add some columns) which I would like to avoid. – milbr Nov 25 '10 at 12:23
  • 2
    A discriminator column on joined inheritance is not supported from what I remember. Let me google up the hibernate doc for you. – Jinesh Parekh Nov 25 '10 at 12:29