每个具体类继承混合联接和表 [英] Mixing Joined and Table per concrete class inheritance
问题描述
我有A,B,C,D,E和F类,其中 B从A延伸,C从B延伸 D从A延伸,E从D延伸 F从A延伸.
I have classes A, B, C, D, E and F where B extends from A, C extends from B D extends from A, E extends from D F extends from A.
我想根据B,D和F级别的每个具体类继承策略在A和Table使用Joined继承策略. A,B和D是抽象类,而C,E和F是具体类.
I want to use Joined inheritance strategy at A and Table per concrete class inheritance strategy from B,D and F level. A,B and D are abstract classes and C,E and F are concrete classes.
这有可能吗,如果是的话,我们应该怎么做.当我尝试时,最终会为所有6个类获得单独的表,并且我希望创建4个表(一个分别用于A,三个分别用于C,E和F).
Is this possible and if yes how should we do it. When I try, I end up getting separate tables for all 6 classes and I want 4 tables to get created (one for A and 3 for C,E and F respectively).
我在类A中使用了@Inheritance(strategy = InheritanceType.JOINED) B,D和F类中的@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
I used @Inheritance(strategy = InheritanceType.JOINED) in class A and @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) in classes B,D and F
我看到了一些示例,将每个层次结构的表混合在一起,并加入了继承策略,但是我想实现上面提到的内容而不使用每个层次结构的表.
I have seen examples to mix table per hierarchy and joined inheritance strategies but I want to implement what I mentioned above without using table per hierarchy.
请帮帮我.
推荐答案
这有点晚了,但是最近我遇到了类似的问题.我发现的解决方案是在不需要单独表的抽象类上使用@MappedSuperclass批注.
This is a bit late, but I had a similar problem recently. The solution that I found is to use @MappedSuperclass annotation on the abstract classes for which you don't want separate tables.
例如
@Entity(name = "A")
@Inheritance(strategy = InheritanceType.JOINED)
public class A {
// mapped fields that go into parent A table
}
@MappedSuperclass
public class B extends A {
// fields that should get pushed down to tables that inherit from B
}
@MappedSuperclass
public class D extends A {
// fields that should get pushed down to tables that inherit from D
}
@Entity(name = "F"
public class F extends A {
// fields that are specific to table F
}
@Entity(name = "C")
public class C extends B {
// fields that are specific to table C
}
@Entity(name = "E")
public class E extends D {
// fields that are specific to table E
}
这假定您在@MappedSuperclass
类中应用的映射(尤其是列名)对于子类表是相同的.如果不是,则需要在类上使用@AttributeOverride
批注.例如,
This assumes that the mappings that you apply in the @MappedSuperclass
classes (in particular, the column names) are the same for the subclass tables. If they aren't, you need to use the @AttributeOverride
annotation on the class. For example,
@MappedSuperclass // this insures that no table is created for this class, but that the mapped fields are pushed down to the subclasses.
public class B extends A {
@Id @Generated
@Column(name = "myId")
public int myId;
}
@Entity(name = "C")
@AttributeOverride(name = "myId", column = @Column(name = "ID"))
public class C extends B {
}
这篇关于每个具体类继承混合联接和表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!