Spring Data JPA:java.sql.SQLException:列'id'找不到

时间:2019-08-30 15:58:19

标签: spring-boot spring-data-jpa

尝试从名为make的列返回列表会导致在控制台中找不到列id的错误。表格中有名为id的列,但不确定为什么无法识别。

Car.java

 package com.example.demo;


import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Car {

    @Id
    private String id;

    private String make;
    private String model;
    private String year;


    public Car() {
    }

    public Car(String id, String make, String model, String year) {
        this.id = id;
        this.make = make;
        this.model = model;
        this.year = year;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }
}

CarRepository.java

package com.example.demo;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CarRepository extends JpaRepository<Car, String> {
    @Query(value="SELECT make FROM car", nativeQuery=true)
    List<Car> getAllMakes();
}

CarController.java

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;


@Controller
public class CarController {



    @Autowired
    private CarService carService;

    @Autowired
    private CarRepository carRepository;



    @GetMapping("/car")
    public String carForm(Model model) {
        model.addAttribute("car", new Car());
        model.addAttribute("cars", carService.getAllCars());
        return "car";
    }

    @PostMapping("/car")
        carService.addCar(car);
        return "redirect:/car";
    }



    @GetMapping("/list")
    public String carMakeList(Model model){
        model.addAttribute("list", carRepository.getAllMakes());
        return "list";
    }


}

list.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Car List</h1>

<table>
    <thead>
    <tr>
        <th>Id</th>
        <th>Make</th>
        <th>Model</th>
        <th>Year</th>
    </tr>
    </thead>
    <tbody>
    <tr th:if="${!list}">
        <td colspan="2"> No Cars Available</td>
    </tr>

    <tr th:each="car : ${list}">

        <td><span th:text="${car.make}">Make</span></td>

    </tbody>
</table>
</body>
2019-08-30 10:12:31.008 ERROR 26538 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'id' not found.
2019-08-30 10:12:31.026 ERROR 26538 --- [nio-8080-exec-1] 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 make FROM car]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause

java.sql.SQLException: Column 'id' not found.

1 个答案:

答案 0 :(得分:2)

您需要在SQL查询中指定*(全部):

public interface CarRepository extends JpaRepository<Car, String> {
    @Query(value="SELECT * FROM car", nativeQuery=true)
    List<Car> getAllMakes();
}

问题是id实体必填Car字段,您也可以尝试:

public interface CarRepository extends JpaRepository<Car, String> {
    @Query(value="SELECT id,make FROM car", nativeQuery=true)
    List<Car> getAllMakes();
}

要使当前查询正常工作,可以使用JPA投影并将代码更改为:

public interface CarRepository extends JpaRepository<Car, String> {
    @Query(value="SELECT make FROM car", nativeQuery=true)
    List<String> getAllMakes();
}