Spring SpEL JPA @Query - 获取带有数字列的 "QueryException: Not all named parameters have been set"
Spring SpEL JPA @Query - getting "QueryException: Not all named parameters have been set" with numeric column
我有一个简单的 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.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'}")
有什么建议吗?
仅供参考:
- 我的 pom.xml.
中有 spring boot 1.3.6.RELEASE、hibernate 4.3.11.Final
- 我正确地 configured/overridden
org.springframework.security.core.userdetails.User 对象传递
userId.
我不确定 SpEL 解析器和 Spring Data JPA 之间是否存在冲突。但我找到了解决方法:
解法:
@Query
上的以下更改效果很好:
@Query("select p from Person p where p.id = ?#{principal.customerId} or 1=?#{hasRole('ROLE_ADMIN') ? 1 : 0}")
我有一个简单的 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.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'}")
有什么建议吗?
仅供参考:
- 我的 pom.xml. 中有 spring boot 1.3.6.RELEASE、hibernate 4.3.11.Final
- 我正确地 configured/overridden org.springframework.security.core.userdetails.User 对象传递 userId.
我不确定 SpEL 解析器和 Spring Data JPA 之间是否存在冲突。但我找到了解决方法:
解法:
@Query
上的以下更改效果很好:
@Query("select p from Person p where p.id = ?#{principal.customerId} or 1=?#{hasRole('ROLE_ADMIN') ? 1 : 0}")