Why am I getting an AnnotationException on Spring Data JPA when trying to create a composite key?

Viewed 43

I don't know if I am doing this right, but am creating an app that assigns tickets to the prom for high school, and for each student, I am having the school put in the ID since all students have a unique ID at that school, and that is being stored as the key. The problem though is that two students at different schools may have the same ID, so I was going to store the primary key as the student ID and school ID. Here is my Student entity class.

  @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;

}

Here is the StudentIdentity class it is referring to.

       @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;
}


} 

I have a School class as well to save information about the school, so that is why I did the one to one relationship, but I haven't been able to find an example online on how to do that, so I'm not sure if I did it right.

I'm getting an exception though.

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: Unable to find properties (studentId, school) in entity annotated with @IdClass:com.promticket.entity.Student

1 Answers

Add :

@Delegate

before

@EmbeddedId
private StudentIdentity studentIdentity;
Related