当前位置: 首页 > 知识库问答 >
问题:

为什么在尝试创建复合键时,我会在Spring Data JPA上得到一个AnnotationException?

冀子石
2023-03-14

我不知道我做得是否正确,但我正在创建一个应用程序,为高中毕业舞会分配门票,对于每个学生,我让学校放入ID中,因为所有学生在该学校都有一个唯一的ID,并作为密钥存储。但问题是,不同学校的两个学生可能有相同的ID,所以我打算将主密钥存储为学生ID和学校ID。这是我的学生实体类。

  @Data
  @AllArgsConstructor
  @NoArgsConstructor
  @Entity
  @Table(name="students")
  @IdClass(StudentIdentity.class)
  public class Student {
 
@EmbeddedId
private StudentIdentity studentIdentity;
 
 
@Column(name= "first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
 
private String status;
 
@Column(name="is_restricted")
private boolean isRestricted;
 
@Column(name="restricted_reason")
private String restrictedReason;
 
@Column(name="ticket_number")
private String ticketNumber;

}
       @Data
   @AllArgsConstructor
    @NoArgsConstructor

    @EqualsAndHashCode


    @Embeddable
    public class StudentIdentity implements Serializable{

public StudentIdentity() {
    
}

private String studentId;


@OneToOne(fetch= FetchType.EAGER)
@JoinColumn(name="school_id", referencedColumnName="id")
private School school;






public School getSchool() {
    return school;
}
public void setSchool(School school) {
    this.school = school;
}
public String getStudentId() {
    return studentId;
}
public void setStudentId(String studentId) {
    this.studentId = studentId;
}


} 

beanCreationException:创建类路径资源[org/springframework/boot/autoconfigure/orm/jpa/hibernatejpaconfiguration.class]中定义的名为“Entity ManagerFactory”的bean时出错:调用init方法失败;嵌套异常是org.hibernate.annotationexception:无法在用@idclass:com.promticket.entity.student注释的实体中找到属性(studentId,school)

共有1个答案

聂昱
2023-03-14

加:

@Delegate

之前

@EmbeddedId
private StudentIdentity studentIdentity;
 类似资料: