Reputation: 855
I am using Spring boot MVC application with hibernate ORM.
I have a table as student which has foreign keys from other tables.
Student.java:
@Entity
@Table(name = "STUDENT")
public class student{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long STUDENT_ID;
@OneToOne
@JoinColumn(name="COURSE_ID")
private Long COURSE_ID;
@OneToOne
@JoinColumn(name="ACDYEAR_ID")
private Long ACDYEAR_ID;
@OneToOne
@JoinColumn(name = "SEMESTER_ID")
private Long SEMESTER_ID;
}
StudentRepository:
@Query(value="SELECT * FROM STUDENT INNER JOIN ACADEMICSYEAR ON STUDENT.ACDYEAR_ID = ACADEMICSYEAR.ACDYEAR_ID INNER JOIN ACADEMICSCOURSE ON STUDENT.COURSE_ID = ACADEMICSCOURSE.COURSE_ID where STUDENT_ID=?1",
nativeQuery=true)
public STUDENT findOneID(Long id);
Same query in sql server returns populated academic year table and course table but here it is giving an error like
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.boot.model.STUDENT.COURSE_ID references an unknown entity: java.lang.Long
Upvotes: 0
Views: 1132
Reputation: 8956
Instead of having type of the id of the entity in the association, have the type of the entity. Example
@Entity
@Table(name = "STUDENT")
public class student{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long STUDENT_ID;
@OneToOne
@JoinColumn(name="COURSE_ID")
private Course course;
}
Where Course
is the associated entity in the unidirectional relationship
Upvotes: 0
Reputation: 3028
The reason it does not work its because you are trying to map Long
object as entity
( because of OneToOne,ManyToOne
mapping). you should map each field to another entity instead of mapping it to Long. Or, just fetch the ids
remove all @OneToOne
annotation and JoinColumn
@Column(name="COURSE_ID")
private Long COURSE_ID;
@Column(name="ACDYEAR_ID")
private Long ACDYEAR_ID;
@Column(name = "SEMESTER_ID")
private Long SEMESTER_ID;
Create an entity for each table i.e Course entity
, Semester entity
and ACADEMICSYEAR entityand change the
Long in each place to be that entity
Upvotes: 2