@Embeddable 中的 JPA @ElementCollection

问题描述 投票:0回答:1
@Entity
public class School {
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer schoolId;
...
 @ElementCollection
 @CollectionTable(
            name = "grades",
            joinColumns = @JoinColumn(name = "schoolId")
    )
 private Set<Grade> grades;

}
----------------------------------

@Embeddable
public class Grade {

private int gradeId

@ElementCollection
@CollectionTable(
            name = "Class",
            joinColumns = @JoinColumn(name = "gradeId")
    )
    private Set<Class> classes;

}


------------------------------------

@Embeddable
public class Class {

private int numOfStudent
private int female;
private int mamle;

}


我想要获取映射表。但是,出现此错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Property 'com.School.grades.collection&&element.clases' belongs to an '@Embeddable' class that is contained in an '@ElementCollection' and may not be a '@ElementCollection'

我不确定上面的代码是否正确。我可以做什么来修复这个错误?

尝试使用@secondrytable和@AttributeOverride。 但是..找不到解决方案。

java spring-boot jpa
1个回答
0
投票

集合表没有自己的主键。因此,一个元素集合不能包含在另一个元素集合中。我认为至少类

Grade
具有标识符
gradeId
,您可以将其设为通常的
@Entity
并将
grades
字段映射从
@ElementCollection
更改为
@OneToMany

© www.soinside.com 2019 - 2024. All rights reserved.