skip to Main Content

i’m trying to execute this query inside the spring boot repository class , but console shows the error ‘ column id not found ‘ also the postman shows:

"status": 500,
"error": "Internal Server Error",
"message": "could not execute query; SQL [SELECT etablissement.etab_name , app_user.creatdate_time FROM etablissement JOIN app_user WHERE year(app_user.creatdate_time)= ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query",

QUERY REPOSITORY

@Query(nativeQuery=true, value="SELECT etablissement.etab_name , app_user.creatdate_time FROM etablissement JOIN app_user WHERE year(app_user.creatdate_time)= :year")
public List<User> findALLUserByyear(@Param("year") String year);

CONTROLLER

@GetMapping(value="/etablissementAlls/{year}")
public EtablissementDto EtabDTOALL(@PathVariable String year) {
    EtablissementDto a = new EtablissementDto();
    
    a.setUsers(userRepository.findALLUserByyear(year));
            
    return a;
}

Stack Trace

2021-05-05 11:14:17.600  WARN 5240 --- [nio-8020-exec-2] org.club.config.JwtRequestFilter         : JWT Token does not begin with Bearer String
2021-05-05 11:14:17.604  WARN 5240 --- [nio-8020-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: S0022
2021-05-05 11:14:17.604 ERROR 5240 --- [nio-8020-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'id' not found.
2021-05-05 11:14:17.606 ERROR 5240 --- [nio-8020-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 etablissement.etab_name , app_user.creatdate_time FROM etablissement JOIN app_user WHERE year(app_user.creatdate_time)= ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause

java.sql.SQLException: Column 'id' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1080) ~[mysql-connector-java-5.1.46.jar:5.1.46]

NOTE

I tested this query in MYSQL PHPmyAdmin, and it works fine

USER ENTITY

@Entity
@Table(name = "app_user")

    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;
        @Column(name = "is_active")
        private boolean active;    
        @JsonIgnore 
        @ManyToMany(fetch = FetchType.EAGER)
        @Fetch(value = FetchMode.SUBSELECT)
        @JoinTable(name = "user_etablissement", joinColumns
                = @JoinColumn(name = "user_id",
                referencedColumnName = "id"),
                inverseJoinColumns = @JoinColumn(name = "etablissement_id",
                        referencedColumnName = "id"))
        private List<Etablissement> etablissements;

2

Answers


  1. Clarify few things:

    1. The MYSQL PHPmyAdmin you have queried on and the service you are connecting database to are pointing to the same database server?
    2. Have you checked manually if ‘id’ column is created in the tables you are querying to?
    Login or Signup to reply.
  2. You should change your query if you have create_time field in User entity

    @Query(value="SELECT u from User u where year(u.creatdate_time)=:year")
    public List<User> findALLUserByyear(@Param("year") String year);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search