首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MappedSuperclass的遗传关系

MappedSuperclass的遗传关系
EN

Stack Overflow用户
提问于 2021-01-03 13:53:37
回答 2查看 42关注 0票数 1

我创建了一个名为“MappedSuperclass”的CommonClass,它包含大量其他类的一些公共属性。此外,我需要该类指定公共关系,例如与另一个名为"RelationClass“的类。

我这样说:

代码语言:javascript
复制
/* ##### 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 ##### */

问题是没有将CommonClass识别为一个实体,这是正确的,但它是一个MappedSuperClass,所以逻辑上应该让子类继承该关系。

这是我得到的错误:

代码语言:javascript
复制
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]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-03 15:41:35

所以这似乎是不可能的。正如下面的链接所提到的:“映射超类的主要缺点是它们不能被查询或持久化。您也不能与映射的超类有关系……”

https://en.wikibooks.org/wiki/Java_Persistence/Inheritance

票数 1
EN

Stack Overflow用户

发布于 2021-01-03 14:48:01

,但是它是一个MappedSuperClass,所以从逻辑上讲,它应该使子类继承这个关系。

我认为原因是Hibernate (或任何JPA实现)在启动阶段不能确定您要添加到@OneToMany列表中的类将肯定是一个实体。甚至可以添加一个从CommonClass扩展而不是@Entity的Java类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65550579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档