0

Trying to return list from column named make causes error in console where column id is not found. The column named id is on the table but not sure why it is not recognized.

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. 
2
  • Maybe you should make it to be of type Integer instead of String? Commented Aug 30, 2019 at 16:03
  • you are not selecting make from the entity. You are no selecting id column Commented Aug 30, 2019 at 16:06

1 Answer 1

6

You need to specify *(all) in your SQL query:

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

The problem is id field is required for Car entity, you can also try:

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

For your current query to work, you can use JPA projections and change your code to:

public interface CarRepository extends JpaRepository<Car, String> { @Query(value="SELECT make FROM car", nativeQuery=true) List<String> getAllMakes(); } 
Sign up to request clarification or add additional context in comments.

5 Comments

thank you. The first query is working but puts the list of make under the column header id on my list.html instead of the column header under make. How does using * instead of make work here? How does it know I'm wanting to print all the makes when using *?
List<Car> getAllMakes(); what is this doing exactly?
Ok, I see why just make column was shown in view... because of my commenting. How would I just get it to print a specified column only and not the complete table please?
sorry, I'm not following. You can specify any column in .html template
Thank you very much. I faced with the same problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.