Java Hibernate java.lang.IllegalArgumentException:参数位置未知:1

时间:2018-09-05 14:47:27

标签: java spring hibernate jpa hql

这是我的查询方法:

@Query(value = "select new com.twognation.hub.dto.GameDTO(game.id, game.name, game.active, game.description, game.coverImage, game.logo, game.backgroundImage, game.featuredImage, game.characterImage, game.smallCoverImage, count(tournament.id) as tournamentCount) from Game game left join Tournament tournament on game.id=tournament.game where lower(game.name) like '%:name%' group by game.id order by tournamentCount desc, game.id",
                countQuery = "select count(1) from Game game left join Tournament tournament on game.id=tournament.game where lower(game.name) like '%:name%' group by game.id order by tournamentCount desc, game.id")
Page<GameDTO> findAllOrderByTournamentCount(@Param("name") String name, Pageable page);

当我调用此方法时,出现此错误:

Caused by: java.lang.IllegalArgumentException: Unknown parameter position: 1    
    at org.hibernate.query.internal.QueryParameterBindingsImpl.getBinding(QueryParameterBindingsImpl.java:240)  
    at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:503)  
    at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:104)  
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:141)     
    at org.springframework.data.jpa.repository.query.StringQueryParameterBinder.bind(StringQueryParameterBinder.java:61)    
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:101)     
    at org.springframework.data.jpa.repository.query.SpelExpressionStringQueryParameterBinder.bind(SpelExpressionStringQueryParameterBinder.java:76)    
    at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:161)   
    at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:152)   
    at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.doCreateQuery(AbstractStringBasedJpaQuery.java:81)     
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createQuery(AbstractJpaQuery.java:190)    
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.doExecute(JpaQueryExecution.java:186)     
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:87)   
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)  
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)    
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:492)     
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:475)   
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)    
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)    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:282)  
    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)   
    ... 172 common frames omitted

2 个答案:

答案 0 :(得分:1)

此:

'%:name%'

在JPA中,本地字符串是唯一的,要解决此问题,您可以像这样使用CONCAT运算符:

CONCAT('%', :name, '%')

例如,稍后将其转换为'%some_value%'

答案 1 :(得分:1)

您似乎对like有疑问。

代替

'%:name%'

这样做:

%:name%