yanf yanf - 1 year ago 71
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 Source

this is working for me:


@DiscriminatorColumn(name = "TYPE_DISCRIMINATOR")
public class A extends SomeClass implements SomeInteface {

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

Notice that "SomeClass" is not an entity.

Subclass - "JOIN" inheritance:

@SecondaryTable(name = "A_SECOND_TABLE", pkJoinColumns = @PrimaryKeyJoinColumn(name ="ID") )
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:

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

Notice the table value.

Subclass – single table inheritance:

public class C extends A implements SomeIntefaceC {...

simple single table inheritance.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download