问题描述
如何在同一继承树中将单表类型继承与连接表类型混合使用?
我没有使用hibernate只是JPA。
我知道通过阅读JPA规范,没有官方支持混合继承。
我无法改变架构。它确实与hibernate一起使用,但现在我需要使用openjpa来实现它。
我正在寻找一些解决方法。
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.
推荐答案
这对我有用:
超类:
@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();
}
…
请注意,SomeClass不是实体。
Notice that "SomeClass" is not an entity.
子类 - JOIN继承:
Subclass - "JOIN" inheritance:
@Entity
@SecondaryTable(name = "A_SECOND_TABLE", pkJoinColumns = @PrimaryKeyJoinColumn(name ="ID") )
@DiscriminatorValue("BD")
public class B extends A implements SomeIntefaceB {
…
创建一个新表A_SECOND_TABLE,加入超类主键ID。
每个不在连接列中且出现在我们表格中的字段标记如下:
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)
注意表值。
子类 - 单表继承:
@Entity
public class C extends A implements SomeIntefaceC {...
简单的单表继承。
这篇关于如何在JPA中混合继承类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!