skip to Main Content
package com.example.demo.Repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.example.demo.entity.WomenCareEntity;

@Repository
public interface WomenCareRepository extends JpaRepository<WomenCareEntity, Integer> {
    @Query(value="select lakh_5 from women_1A_premium where :age >=min_age and :age <=max_age and year=1",nativeQuery=true)
    int findPremium(int age);
"message": "JDBC exception executing SQL [select lakh_5 from women_1A_premium where ? >=min_age and ? <=max_age and year=1]; SQL [n/a]".

This is the error message. What do I need to do?

2

Answers


  1. If you are using "Named Parameters" in the query like :age in your case, then you need to add "@Param" annotation to give a method parameter a concrete name and bind the name in the query. Modify the query like below:

    @Query(value="select lakh_5 from women_1A_premium where :age >=min_age and :age <=max_age and year=1",nativeQuery=true)
    int findPremium(@Param("age") int age);
    

    spring jpa query doc: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.named-parameters

    Login or Signup to reply.
  2. It need an extra annotation in function parameter.

    Correct one is as below:

    @Repository
    public interface WomenCareRepository extends JpaRepository<WomenCareEntity, Integer> {
        
            @Query(value="select lakh_5 from women_1A_premium where :age >=min_age and :age <=max_age and year=1",nativeQuery=true)
            int findPremium(@Param("age")  int age);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search