milbr
milbr

Reputation: 1025

Discriminator in InheritanceType.JOINED

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.

Upvotes: 20

Views: 23993

Answers (2)

BadgerB
BadgerB

Reputation: 306

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.

Upvotes: 15

Jinesh Parekh
Jinesh Parekh

Reputation: 2141

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

Upvotes: 1

Related Questions