使用 Spring Boot 对数据库进行错误查询

我的 API 中的 @query 有一些问题。在我的数据库中,我有 1 个包含 5 列的表:id、imie、nazwisko、wiek、miasto。


我正在尝试对我的数据库进行自定义查询,然后出现 -> 错误。


当我像这样创建 @Query 时:


@Query(value = "SELECT * FROM osoba WHERE osoba.id = :id", nativeQuery = true)

结果:


{

    "id": 1,

    "imie": "Szymon",

    "nazwisko": "Markowski",

    "wiek": 55,

    "miasto": "Sosnowiec"

}

并且知道我正在尝试进行另一个查询,例如:


@Query(value = "SELECT imie  FROM osoba WHERE osoba.id = :id", nativeQuery = true)

现在我们开始。它会产生如下问题:


2019-09-10 21:44:29.141 ERROR 8068 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'id' not found.

2019-09-10 21:44:29.158 ERROR 8068 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [SELECT imie  FROM osoba WHERE osoba.id = ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause

有任何想法吗?


繁星点点滴滴
浏览 119回答 2
2回答

慕雪6442864

您的问题是您的存储库不知道您返回的列类型以及如何将它们与您的实体匹配。首先,您需要更改自定义方法的返回类型,如下所示:@Repositorypublic interface OsobaRepository extend JpaRepository<Osoba, Integer> {&nbsp; &nbsp;@Query(value = "SELECT imie FROM osoba WHERE osoba.id = :id",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nativeQuery = true)&nbsp;&nbsp; &nbsp;String findNameById(@Param("id") int id);}现在,如果您想将结果从String转为Json,在您的OsobaServiceImpl(如果存在)中,您需要创建该类的一个实例Osoba,填充它并返回到控制器,如下所示:@Servicepublic ServiceOsobaImpl implementation ServiceOsoba {&nbsp; &nbsp; private final OsobaRepository osobaRepository;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; public ServiceOsobaImpl(OsobaRepository osobaRepository) {&nbsp; &nbsp; &nbsp; &nbsp; this.osobaRepository = osobaRepository;&nbsp; &nbsp; }&nbsp; &nbsp; // your methods&nbsp; &nbsp; ...&nbsp; &nbsp; public Osoba findNameById(int id) {&nbsp; &nbsp; &nbsp; &nbsp; String name = osobaRepository.findNameById(id);&nbsp; &nbsp; &nbsp; &nbsp; Osoba osoba = new Osoba();&nbsp; &nbsp; &nbsp; &nbsp; osoba.setName(name);&nbsp; &nbsp; &nbsp; &nbsp; return osoba;&nbsp; &nbsp; }}还有另一种选择,当您使用一个字段创建简单OsobaDto并使用它时(类比之前的选项):public OsobaDto implements Serializable {&nbsp; &nbsp; private String name;&nbsp; &nbsp; public OsobaDto() {}&nbsp; &nbsp; //getter and setter&nbsp;}@Servicepublic ServiceOsobaImpl implementation ServiceOsoba {&nbsp; &nbsp; private final OsobaRepository osobaRepository;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; public ServiceOsobaImpl(OsobaRepository osobaRepository) {&nbsp; &nbsp; &nbsp; &nbsp; this.osobaRepository = osobaRepository;&nbsp; &nbsp; }&nbsp; &nbsp; // your methods&nbsp; &nbsp; ...&nbsp; &nbsp; public OsobaDto findNameById(int id) {&nbsp; &nbsp; &nbsp; &nbsp; String name = osobaRepository.findNameById(id);&nbsp; &nbsp; &nbsp; &nbsp; OsobaDto osobaDto = new OsobaDto();&nbsp; &nbsp; &nbsp; &nbsp; osobaDto.setName(name);&nbsp; &nbsp; &nbsp; &nbsp; return osobaDto;&nbsp; &nbsp; }}两个选项响应均为 json 格式。produces = MediaType.APPLICATION_JSON_VALUE当然,如果您在控制器方法上方指定(或拥有类似的变体)。更新。如果您使用Osoba类并且不想看到null价值,请执行以下操作。创建新界面:public interface OnlyName {}下一步是将您的实体标记为@Validated并将字段注释imie为@JsonView:@Entity@Validatedpublic class Osoba {&nbsp; &nbsp;// your code&nbsp; &nbsp;@JsonView(OnlyName.class)&nbsp; &nbsp;private String imie;}和控制器:@RestController@RequestMapping("/api/osoba")public class OsobaAPI {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private OsobaRepo osobaRepo;&nbsp; &nbsp; @JsonView(OnlyName.class)&nbsp; &nbsp; @PostMapping&nbsp; &nbsp; @RequestMapping("/getID")&nbsp; &nbsp; public Osoba findOsobaById(@RequestParam("id") int id){&nbsp; &nbsp; &nbsp; &nbsp; String name = osobaRepo.findNameById(id);&nbsp; &nbsp; &nbsp; &nbsp; Osoba osoba = new Osoba();&nbsp; &nbsp; &nbsp; &nbsp; osoba.setImie(name);&nbsp; &nbsp; &nbsp; &nbsp; return osoba;&nbsp; &nbsp; }}

MMTTMM

我确实像你说的那样,我有类似的东西:@Repositorypublic interface OsobaRepo extends CrudRepository<Osoba, Integer> {&nbsp; &nbsp; @Query(value = "SELECT imie FROM osoba WHERE osoba.id = :id", nativeQuery = true)&nbsp; &nbsp; String findNameById(@Param("id") int id);}@RestController@RequestMapping("/api/osoba")public class OsobaAPI {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private OsobaRepo osobaRepo;&nbsp; &nbsp; @PostMapping&nbsp; &nbsp; @RequestMapping("/getID")&nbsp; &nbsp; public Osoba findOsobaById(@RequestParam("id") int id){&nbsp; &nbsp; &nbsp; &nbsp; String name = osobaRepo.findNameById(id);&nbsp; &nbsp; &nbsp; &nbsp; Osoba osoba = new Osoba();&nbsp; &nbsp; &nbsp; &nbsp; osoba.setImie(name);&nbsp; &nbsp; &nbsp; &nbsp; return osoba;&nbsp; &nbsp; }}@Entitypublic class Osoba {&nbsp; &nbsp; @Id&nbsp; &nbsp; @GeneratedValue(strategy = GenerationType.IDENTITY)&nbsp; &nbsp; private int id;&nbsp; &nbsp; private String imie;&nbsp; &nbsp; private String nazwisko;&nbsp; &nbsp; private int wiek;&nbsp; &nbsp; private String miasto;&nbsp; &nbsp; public Osoba() {&nbsp; &nbsp; }&nbsp; &nbsp; public Osoba(String imie, String nazwisko) {&nbsp; &nbsp; &nbsp; &nbsp; this.imie = imie;&nbsp; &nbsp; &nbsp; &nbsp; this.nazwisko = nazwisko;&nbsp; &nbsp; }&nbsp; &nbsp; public Osoba(int id, String imie, String nazwisko, int wiek, String miasto) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; this.imie = imie;&nbsp; &nbsp; &nbsp; &nbsp; this.nazwisko = nazwisko;&nbsp; &nbsp; &nbsp; &nbsp; this.wiek = wiek;&nbsp; &nbsp; &nbsp; &nbsp; this.miasto = miasto;&nbsp; &nbsp; }&nbsp; &nbsp; public int getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(int id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; public String getImie() {&nbsp; &nbsp; &nbsp; &nbsp; return imie;&nbsp; &nbsp; }&nbsp; &nbsp; public void setImie(String imie) {&nbsp; &nbsp; &nbsp; &nbsp; this.imie = imie;&nbsp; &nbsp; }&nbsp; &nbsp; public String getNazwisko() {&nbsp; &nbsp; &nbsp; &nbsp; return nazwisko;&nbsp; &nbsp; }&nbsp; &nbsp; public void setNazwisko(String nazwisko) {&nbsp; &nbsp; &nbsp; &nbsp; this.nazwisko = nazwisko;&nbsp; &nbsp; }&nbsp; &nbsp; public int getWiek() {&nbsp; &nbsp; &nbsp; &nbsp; return wiek;&nbsp; &nbsp; }&nbsp; &nbsp; public void setWiek(int wiek) {&nbsp; &nbsp; &nbsp; &nbsp; this.wiek = wiek;&nbsp; &nbsp; }&nbsp; &nbsp; public String getMiasto() {&nbsp; &nbsp; &nbsp; &nbsp; return miasto;&nbsp; &nbsp; }&nbsp; &nbsp; public void setMiasto(String miasto) {&nbsp; &nbsp; &nbsp; &nbsp; this.miasto = miasto;&nbsp; &nbsp; }}POSTMAN 的原因是:{&nbsp; &nbsp; "id": 0,&nbsp; &nbsp; "imie": "Szymon",&nbsp; &nbsp; "nazwisko": null,&nbsp; &nbsp; "wiek": 0,&nbsp; &nbsp; "miasto": null}但希望从这个查询中有理由,比如{&nbsp; &nbsp; "imie": "Szymon",}编辑:关于这个话题还有一个问题。如果我想获得 JSON 格式的响应,例如:“imie,”nazwisko”?我不能使用:&nbsp; &nbsp; @Query(value = "SELECT imie, nazwisko FROM osoba WHERE osoba.id = :id", nativeQuery = true)&nbsp; &nbsp; String findNameById(@Param("id") int id);}如果作为字符串,则导致响应。当我这样做时,我得到:{&nbsp; &nbsp; "imie": null,&nbsp; &nbsp; "nazwisko": "IMIE, NAZWISKO"}也许我应该在这里使用 String[] 或创建仅具有这两个属性的新类?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java