Java 从MappedSuperclass继承关系
我创建了一个名为“CommonClass”的MappedSuperclass,其中包含大量其他类的一些commons属性。此外,我需要该类指定公共关系,例如与另一个名为“RelationClass”的类 我这样说:Java 从MappedSuperclass继承关系,java,spring,hibernate,jpa,inheritance,Java,Spring,Hibernate,Jpa,Inheritance,我创建了一个名为“CommonClass”的MappedSuperclass,其中包含大量其他类的一些commons属性。此外,我需要该类指定公共关系,例如与另一个名为“RelationClass”的类 我这样说: /* ##### CommonClass ##### */ @MappedSuperclass public class CommonClass { @Id @GeneratedValue(strategy = GenerationType.AUTO) pr
/* ##### 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 ##### */
但是它是一个MappedSuperClass,所以逻辑上它应该让子类继承这个关系
我认为原因是Hibernate(或JPA实现的任何东西)在启动阶段无法确定您将添加到
@OneToMany
列表中的类将肯定是一个实体。甚至可以添加一个Java类,该类扩展自CommonClass
,但不是@实体
,因此似乎不可能。如以下链接所述:
“映射超类的主要缺点是它们不能被查询或持久化。您也不能与映射超类建立关系。…”
我想你不应该写
List
而应该写List
如果我这样做,就像我直接把关系放在ChildClass上一样。其实我小时候有8节课,所以。。。将这些关系放在它们各自的逻辑上会更容易,有效地我们可以从CommonClass扩展一个非实体类。。。你知道我怎样才能把继承和关系结合起来吗?
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]