Spring SpEL JPA @Query - 获取" QueryException:并未设置所有命名参数"使用数字列

时间:2016-08-09 13:47:14

标签: java spring hibernate spring-data-jpa spring-el

我是一个有两个字段的简单Person实体:

@Entity
@Table(name = "person")
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "name", length = 50)
    private String name;

    //setter/getters
}

我在JpaRepository中覆盖findAll方法以根据用户角色限制记录:

public interface PersonRepository extends JpaRepository<Person,Long> {

    @Query("select p from Person p where p.name like ?#{hasRole('ROLE_ADMIN') ? '%' : principal.username}")
    @Override
    List<Person> findAll();
}

以上设置工作正常。但是当我将查询更新为以下内容时,它会抛出异常,如下所示。它看起来无法解析SpEL表达式。

有问题的查询:

@Query("select p from Person p where p.id like ?#{hasRole('ROLE_ADMIN') ? '%' : principal.userId}")

例外:

Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: Not all named parameters have been set: [1] [select p from Person p where p.id like ?1]
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:458)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:114)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:78)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:100)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:91)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:462)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:440)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    ... 142 common frames omitted
Caused by: org.hibernate.QueryException: Not all named parameters have been set: [1] [select p from Person p where p.id like ?1]
    at org.hibernate.internal.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:401)
    at org.hibernate.internal.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:385)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:99)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
    ... 156 common frames omitted

我玩了它并尝试了以下选项,但得到了同样的例外:

1)用=

替换LIKE

@Query("select p from Person p where p.id = ?#{hasRole('ROLE_ADMIN') ? '1' : principal.userId}")

2)设置硬编码ID:

@Query("select p from Person p where p.id = ?#{hasRole('ROLE_ADMIN') ? '6' : '5'}")

有什么建议吗?

仅供参考:

  • 我的春季启动1.3.6.RELEASE,hibernate 4.3.11.Final在我的pom.xml中。
  • 我已经正确配置/覆盖了 org.springframework.security.core.userdetails.User对象传递 用户ID。

1 个答案:

答案 0 :(得分:1)

我不确定SpEL解析器和Spring Data JPA之间是否存在冲突。但我发现了一个解决方法:

<强>解决方案: @Query上的以下更改工作正常:

@Query("select p from Person p where p.id = ?#{principal.customerId} or 1=?#{hasRole('ROLE_ADMIN') ? 1 : 0}")