" java.lang.IllegalArgumentException:具有该位置[1]的参数不存在"当我使用spring-data-jpa时

时间:2015-01-14 06:20:10

标签: java hibernate spring-data-jpa

我正在使用 spring-data-jpa + hibernate 。 我碰到下面的例外 ......

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. 我认为异常来自

    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);
    }
    
  2. 因为在我从函数findExps的params中删除了规范规范后,它运行良好,我只是想为查询添加规范。

    BYW,FamousExperience类扩展了BriefInfomation。我使用了JPA Query的“构造函数表达式”功能,当我尝试查询时,我只是不需要属性“content”。

    @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;
        }
    }
    

    请帮助我!

1 个答案:

答案 0 :(得分:0)

如果你有规范规范,你需要在你的查询中的某个地方引用它...重要的是,当你有一个可分页的时候,那么计数器不会从?0开始但是在?1 < / p>

所以这样的事情应该有效

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