I have two tables Student
and Student_Type
, Each student have each type, while selecting student table how to get the details Student_Type in Spring data JPA.
Student Enity
@Enity
@Table(schema="DATA", name="STUDENT")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="fname")
private string fanme;
@Column(name="lname")
private string lanme;
@Transiant
private string config_type; // Studen_Type type should be populate on here
}
Student_Type Enity
@Enity
@Table(schema="DATA", name="STUDENT_TYPE")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StudentType {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="type")
private string type;
@Column(name="stud_id") // Student `id` is referenced
private string stud_id;
@OneToOne(optional=false)
@JoinCoulumn(name="stud_id" referencedColumnName= "id" insertable=false, updateble=false)
private Student student;
}
While doing select query of student
table how to get the type
from Student_type
table.
Sample data of Student
table
| Id | fname | lname|
| --- | ----- |----|
| 1 | Raju | Mikel |
| 2 | Thomas | George |
| 3 | Arun | Nair |
Sample data of Student_Type
table
| Id | type | stud_id|
| --- | ----- |----|
| 1 | Performer | 1 |
| 2 | Avg | 2 |
| 3 | Performer | 3 |
I'm new in Spring data JPA,
I tried the One to One mapping but its not working, This is my native query,
StudentRepository
@Query("select s.fname, s.lanme, st.type as config_type from student s left join student_type st on s.id=st.stud_id", nativeQuery=true)
List<Student> findStudetType();
Please help that would be appreciated
In your Student
, add:
@OneToOne(mappedBy="student")
private StudentType tudentType;
Secondly, change your query to JPQL:
@Query("SELECT s FROM Student s LEFT JOIN FETCH StudentType st")