'"java.lang.IllegalArgumentException: Parameter with that position [1] did not exist" When I use spring-data-jpa

I am using spring-data-jpa+hibernate. 1.I run into below exception ......

Caused by: java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
    at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:518) ~[BaseQueryImpl.class:4.3.7.Final]
    at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:674) ~[BaseQueryImpl.class:4.3.7.Final]
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:198) ~[AbstractQueryImpl.class:4.3.7.Final]
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:49) ~[AbstractQueryImpl.class:4.3.7.Final]
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:165) ~[ParameterBinder.class:?]
    at org.springframework.data.jpa.repository.query.StringQueryParameterBinder.bind(StringQueryParameterBinder.java:66) ~[StringQueryParameterBinder.class:?]

......

  1. I believe the exception comes from

    public interface FamousExperienceDao extends PagingAndSortingRepository<FamousExperience,
      Long>,JpaSpecificationExecutor<FamousExperience>
    {
        @Query( value = 
            "select new com.tujia.community.entity.BriefInfomation(f.id,f.title,f.summary,f.thumbnail,f.author, f.issueDate, f.counter) from FamousExperience f"
            ,countQuery ="select count(f.id) from FamousExperience f")
        public Page<BriefInfomation> findExps(Specification<FamousExperience> spec, Pageable pgbl);
    }
    

because after I get rid of Specification spec from params of function findExps, it works well, I just want to add specification for the query.

BYW, class FamousExperience extends BriefInfomation. I used "constructor expressions" feature of JPA Query and I just don't need property "content" when I try to query.

@Entity
@Table(name = "famous_experience")
public class FamousExperience extends BriefInfomation
    {
    private String content;

    /**
     * @return the content
     */
    public String getContent()
    {
        return content;
    }

    /**
     * @param content the content to set
     */
    public void setContent(String content)
    {
        this.content = content;
    }
}

Pls help me!



Solution 1:[1]

if you have Specification spec in there you will need to refer it somewhere in your query... the important thing is that it seems like when you have a pageable there then the counter doesnt start at ?0 but at ?1

so something like this should work

 @Query( value = 
    "from FamousExperience f where f.spec = ?1"
    ,countQuery ="select count(f.id) from FamousExperience f")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Christopher Rivera