yanf yanf - 19 days ago 5
Java Question

How to mix inheritance type in JPA

How to mix single table type inheritance with join table type in the same inheritance tree?
I'm not using hibernate just JPA.
I know there is no official support for mixed inheritance by reading the JPA spec.
I can't change the architecture. It did worked with hibernate but now I need to implement this using openjpa.
I'm looking for some workaround.

Answer

this is working for me:

Superclass:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE_DISCRIMINATOR")
public class A extends SomeClass implements SomeInteface {


…
@Id
@Column(name = "ID", nullable = false, precision = 0)
public Integer getPk() {
    return super.getPk();
}
…

Notice that "SomeClass" is not an entity.

Subclass - "JOIN" inheritance:

@Entity
@SecondaryTable(name = "A_SECOND_TABLE", pkJoinColumns = @PrimaryKeyJoinColumn(name ="ID") )
@DiscriminatorValue("BD")
public class B extends A implements SomeIntefaceB {
…

Create a new table "A_SECOND_TABLE" with join on super class Primary Key "ID". each field that is not in join column and appears in our table is marked like this:

@Basic
@Column(table = "A_SECOND_TABLE", name = "STATUS", nullable = false, precision = 0)

Notice the table value.

Subclass – single table inheritance:

@Entity
public class C extends A implements SomeIntefaceC {...

simple single table inheritance.