datajpa只能取得左联接表的数据,右表数据拿不到

先看看实体如下
@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;


表1的tid等于表2的id,然后在jpa里面写原生sql

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();

}

再然后调用
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

}]
数据如上,然后consloe出来的sql如下
Hibernate: 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

我复制其进入命令行,却能取出教师表的信息

求教大神,我哪里出错了,万谢!

繁花不似锦
浏览 404回答 1
1回答

慕尼黑8549860

你的Student类不就只定义了这几个字段嘛...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java