Discriminator in InheritanceType.JOINED
Asked Answered
P

2

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.

Purpleness answered 25/11, 2010 at 9:32 Comment(1)
After further search looks like it's not supporter by Hibernate annotations: opensource.atlassian.com/projects/hibernate/browse/ANN-140Purpleness
S
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.

Shuffle answered 17/10, 2011 at 14:32 Comment(2)
This does not seem irrelevant to me.Chigetai
@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 workingJurado
D
1

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

Dimorphism answered 25/11, 2010 at 11:23 Comment(2)
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.Purpleness
A discriminator column on joined inheritance is not supported from what I remember. Let me google up the hibernate doc for you.Dimorphism

© 2022 - 2024 — McMap. All rights reserved.