我创建了一个名为“MappedSuperclass”的CommonClass,它包含大量其他类的一些公共属性。此外,我需要该类指定公共关系,例如与另一个名为"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 ##### */问题是没有将CommonClass识别为一个实体,这是正确的,但它是一个MappedSuperClass,所以逻辑上应该让子类继承该关系。
这是我得到的错误:
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]发布于 2021-01-03 15:41:35
所以这似乎是不可能的。正如下面的链接所提到的:“映射超类的主要缺点是它们不能被查询或持久化。您也不能与映射的超类有关系……”
发布于 2021-01-03 14:48:01
,但是它是一个MappedSuperClass,所以从逻辑上讲,它应该使子类继承这个关系。
我认为原因是Hibernate (或任何JPA实现)在启动阶段不能确定您要添加到@OneToMany列表中的类将肯定是一个实体。甚至可以添加一个从CommonClass扩展而不是@Entity的Java类。
https://stackoverflow.com/questions/65550579
复制相似问题