简体   繁体   English

Java-JPA:如何为每个子类而不是超类创建表?

[英]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.

相关问题 如何从 Java 的超类中的 static 方法创建子类的新实例? - How can I create a new instance of a subclass from a static method in its superclass in Java? 如何创建给定超类的每个子类的实例? - How can I create instances of every subclass of a given superclass? 我如何在子类java中使用超类方法 - how can i use a superclass method in a subclass java 我无法使用 super() 在子类中的超类之外创建 2 个对象; - I Can't create 2 objects off superclass in subclass using super(); 如何在运行时根据构造函数参数在超类中创建子类对象(在Java中) - How to create subclass objects in superclass depending on construtor parameters at runtime (in Java) 在Java中,我可以使子类的实例“基于”超类的实例吗? - In Java, can I make an instance of a subclass “based on” an instance of the superclass? 如何在Java子类中添加两个变量以作为一个变量传递给超类? - How can I add two variables in a java subclass to pass as one variable to the superclass? 如何在java中声明可以存储超类和子类对象的数组? - How to declare array that can store superclass and subclass object in java? (OOP / Java)我应该如何处理这个子类 - 超类关系? - (OOP/Java) How should I handle this subclass-superclass relationship? java在子类中以超类为参数创建构造函数 - java create constructor in subclass with superclass as arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM