从mappedsuperclass继承关系

js4nwp54  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(324)

我创建了一个名为“commonclass”的mappedsuperclass,其中包含大量其他类的一些commons属性。另外,我需要该类指定公共关系,例如与另一个名为“relationclass”的类。
我是这样说的:

/* ##### CommonClass ##### */
@MappedSuperclass
public class CommonClass {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    ...

    @ManyToOne
    @JoinColumn(name = "fk_relation_class")
    private RelationClass relationClass;

    // === Class getters & setters ===============================
    ... 
}
/* ##### CommonClass ##### */

/* ##### ChildClass ##### */
@Entity
public class ChildClass extends CommonClass{

    private String specificAttribute;
    ...

    // === Class getters & setters ===============================
    ...
}
/* ##### ChildClass ##### */

/* ##### RelationClass ##### */
@Entity
public class RelationClass {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    ...

    @OneToMany(mappedBy = "relationClass")
    private List<CommonClass> commonList = new ArrayList<CommonClass>();

    // === Class getters & setters ===============================
    ...
}
/* ##### RelationClass ##### */

问题是dosen没有将commonclass识别为实体,这是正确的,但它是一个mapped超类,因此逻辑上应该使子类继承关系。
这是我得到的错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.xxx.dao.entities.RelationClass.objectList[com.xxx.dao.entities.CommonClass]
...
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.xxx.dao.entities.RelationClass.objectList[com.xxx.dao.entities.CommonClass]
9fkzdhlc

9fkzdhlc1#

所以这似乎是不可能的。正如在下面的链接中提到的:“Map超类的主要缺点是它们不能被查询或持久化。您也不能与Map的超类有关系……”
https://en.wikibooks.org/wiki/java_persistence/inheritance

thtygnil

thtygnil2#

但是它是一个mapped超类,所以逻辑上它应该让子类继承这个关系。
我认为原因是hibernate(或者是jpa实现)不能在启动阶段确定要添加到 @OneToMany 名单肯定是一个实体。甚至可以添加一个从 CommonClass 但不是一个 @Entity .

相关问题