[英]Java - JPA: How can I create a table for each subclass but not for superclass?
I have a class structure like: 我有一个类似的类结构:
abstract class A {
String a;
}
class B extends A {
String b;
}
class C extends A {
String c;
}
Now I want JPA to create a table for each subclass. 现在,我希望JPA为每个子类创建一个表。
I have looked some previous questions but I am confused. 我之前看过一些问题,但感到困惑。 How can I map these classes? 如何映射这些类?
If the A is inherited because of mappings and you do not need separate entity for it, you can defined it as MappedSuperClass . 如果A是由于映射而继承的,并且您不需要单独的实体,则可以将其定义为MappedSuperClass 。 Mapped superclass is only for inheriting mappings, you cannot query it. 映射的超类仅用于继承映射,不能查询它。
On the other hand if real inheritance is needed - depending about implementation - you can go for table per (concrete) class inheritance strategy. 另一方面,如果需要真正的继承(取决于实现方式),则可以针对每个(具体)类继承策略使用表。 Support for this strategy is optional, so it is not guaranteed to work with all implementations. 对这种策略的支持是可选的,因此不能保证可以与所有实现一起使用。 It is supported at least with fresh versions of 至少新版本的软件支持该功能
In your case needed step would be then to add following to the class A: @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
在您的情况下,然后需要将以下内容添加到类A: @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
为什么不使用@MappedSuperClass注释A类,将Table子类或Table用于具体类的继承策略
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.