当前位置: 首页 > 知识库问答 >
问题:

Spring SpEL JPA @Query - 获取带有数字列的“QueryException:未设置所有命名参数”

虞俊美
2023-03-14

我有一个带有两个字段的简单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)用=替换赞

@Query("从 = ? #{有角色('ROLE_ADMIN')的人p中选择p?'1':principal.userId}")

2) 设置硬编码 ID:

@Query(“从p.id=?#{hasRole('ROLE_ADMIN')?'6':'5'}中选择p”)

有什么建议吗?

仅供参考:

  • 我有Spring的靴子1.3.6.RELEASE,冬眠4.3.11.Final在我的pom.xml.
  • 我已经正确配置/覆盖了org.springframework.security.core.userdetails.User对象以传递userId。

共有1个答案

夏侯衡
2023-03-14

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

解决方案:对@Query的以下更改工作正常:

@Query(“从 Person p 中选择 p,其中 p.id = ?#{principal.customerId} 或 1=?#{hasRole('ROLE_ADMIN') ?1 : 0}“)

 类似资料: