gpt4 book ai didi

java - JPQL @query 与 json 的关系表

转载 作者:行者123 更新时间:2023-11-30 07:55:56 28 4
gpt4 key购买 nike

我正在使用 @Query 创建 JPQL 查询以获取 JSON 关系数据列表

package thymeleaf.test.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import thymeleaf.test.model.Student;

@RepositoryRestResource
public interface StudentRepo extends CrudRepository<Student, Integer> {

@Query("select s,t.teachName FROM Student s JOIN s.teacher t WHERE s.teacher.teachName= t.teachName")
List<Student> findAll() ;
}

我得到了以下结果:

[
[
{
"stuId": 1,
"stuName": "Pawarut klai-armon ",
"stuAge": 21
},
"sgkyrtgefewd "
]
]

我希望这个查询返回这个:

[
{
"stuId": 1,
"stuName": "Pawarut klai-armon ",
"stuAge": 21
"teachName": "sgkyrtgefewd "
}
]

最佳答案

您需要明确命名要获取的字段,如下所示:

select s.stuId, s.stuName, s.stuAge, t.teachName FROM Student s JOIN s.teacher t WHERE s.teacher.teachName= t.teachName

如果您需要命名字段,则需要像这样定义 DTO 对象:

class StuInfoWithTeacherName {
private long id;
private String stuName;
private int stuAge;
private String teacherName;
public StuInfoWithTeacherName(id, stuName, stuAge, teacherName){
//omitted
}
}

比调用查询类似

select new StuInfoWithTeacherName(s.stuId, s.stuName, s.stuAge, t.teachName) FROM Student s JOIN s.teacher t WHERE s.teacher.teachName= t.teachName

关于java - JPQL @query 与 json 的关系表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32671419/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com