Error with Spring boot JPA with parameters

Viewed 113
//Repository class

@Repository

public interface FlowsPageRepository extends PagingAndSortingRepository<FlowsPageView, String> {
    @Query(name = "select * from flows_page_view f where f.datepst between ?1 and ?2")
    List<FlowsPageView> findScoresByDate(String startDate, String endDate);
 }

//Controller class
 @RestController
 public class ApiController {
    private final FlowsPageRepository flowsPageRepository;

    @RequestMapping(path = "/api/scores/{startdate}/{enddate}")
    public List<FlowsPageView> getScoresDataByDate(@PathVariable("startdate") String 
       startDate, @PathVariable("enddate") String endDate) {

       return flowsPageRepository.findScoresByDate(startDate, endDate);
    }
 }

When I am trying to run this project I am getting the following error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowsPageRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.expedia.fcts.houston.FlowsPageRepository.findScoresByDate(java.lang.String,java.lang.String)! At least 2 parameter(s) provided but only 1 parameter(s) present in query.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)

Any idea what I am doing wrong here? This code runs fine when I use only one parameter like startDate but it gives me this error when I am adding endDate. For this use case I am trying to run the select for a date range.

Thanks

1 Answers

The @Query annotation is using the wrong field. Query#name is there to specify named query. - Query#value should be used in your case instead.

Also by default the query value is parsed as JPQL. If you want to use SQL then you need to specify Query#nativeQuery as true.

Related