【问题标题】:"java.lang.IllegalArgumentException: Parameter with that position [1] did not exist" When I use spring-data-jpa“java.lang.IllegalArgumentException:具有该位置 [1] 的参数不存在”当我使用 spring-data-jpa
【发布时间】:2015-03-12 06:43:43
【问题描述】:

我正在使用 spring-data-jpa+hibernate。 1.我遇到以下异常 ......

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

因为在我从函数 findExps 的参数中去掉了 Specification spec 之后,它工作得很好,我只想为查询添加规范。

BYW,FamousExperience 类扩展了 BriefInfomation。我使用了 JPA 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;
    }
}

请帮帮我!

【问题讨论】:

  • 您不能将规范用作任何自定义方法的常规参数。规范应用于继承自JpaSpecificationExecutor 接口的List&lt;T&gt; findAll(Specification&lt;T&gt; spec);Page&lt;T&gt; findAll(Specification&lt;T&gt; spec, Pageable pageable); 方法

标签: java hibernate spring-data-jpa


【解决方案1】:

如果你有规范规范,你需要在查询中的某个地方引用它......重要的是,当你在那里有一个可分页时,计数器似乎不是从 ?0 开始,而是从 ?1

所以这样的东西应该可以工作

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-22
    • 2019-03-06
    • 2019-07-03
    • 2016-09-11
    • 2018-07-05
    • 2017-10-03
    • 2017-07-29
    • 2017-03-22
    相关资源
    最近更新 更多