first take a look at the entity as follows
@ Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String sname;
private String age;
private Integer tid;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String tname;
1tid2idjpasql
public interface StudentRepository extends JpaRepository<Student, Integer> {
@Query(value = "select s.id,s.sname,s.age,tid,t.id as ttid,t.tname from student s left join teacher t on s.tid=t.id",nativeQuery = true)
public List<Student> allStu();
}
then call
List < Student > students = studentRepository.allStu ();
return students;
[{
"id": 1,
"sname": "",
"age": "11",
"tid": 1
}, {
"id": 2,
"sname": "",
"age": "12",
"tid": 2
}, {
"id": 3,
"sname": "",
"age": "15",
"tid": 1
}, {
"id": 4,
"sname": "",
"age": "12",
"tid": 1
}}]
data is as above, and then the sql from consloe is as follows
Hibernate: select s.idjournal s.snameparents.age as ttid,t.tname from student s left join teacher t on s.tid=t.id. Id as ttid,t.tname from student s left join teacher t on s.tid=t.id
I copy it into the command line, but I can take out the information of the teacher table
ask for advice, what"s wrong with me? thank you!