在游戏实体中,我有:
key_name
我编写了自定义查询,该查询将按类型ID过滤游戏。当我在查询中对流派ID进行硬编码时,它会起作用:
private Set<GameGenre> genres = new HashSet<>();
但是当我尝试传递ID列表时:
@Query("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 inner join game.genres genres where lower(game.name) like CONCAT('%', :name, '%') and genres IN (1301) group by game.id order by tournamentCount desc, game.id")
我收到此错误:
@Query("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 inner join game.genres genres where lower(game.name) like CONCAT('%', :name, '%') and genres IN (:genres) group by game.id order by tournamentCount desc, game.id")
Page<GameDTO> findAllOrderByTournamentCountAndGenreFilter(@Param("name") String name, @Param("genres") String genres, Pageable page);
游戏实体:
Caused by: java.lang.IllegalArgumentException: Parameter value [1301] did not match expected type [com.twognation.hub.domain.GameGenre (n/a)]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:54)
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27)
at org.hibernate.query.internal.QueryParameterBindingImpl.validate(QueryParameterBindingImpl.java:90)
at org.hibernate.query.internal.QueryParameterBindingImpl.setBindValue(QueryParameterBindingImpl.java:55)
at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:486)
at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:104)
at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:139)
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
答案 0 :(得分:1)
您可能需要传递GameGenre类型的对象而不是字符串。也许也是GameGenre对象的集合。
答案 1 :(得分:1)
您的查询应为and genres.genreId IN (:genres)
。
@Query("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 inner join game.genres genres where lower(game.name) like CONCAT('%', :name, '%') and genres.genreId IN (:genres) group by game.id order by tournamentCount desc, game.id")
Page<GameDTO> findAllOrderByTournamentCountAndGenreFilter(@Param("name") String name, @Param("genres") String genres, Pageable page);
genres
是实体,genreId
是映射到genre_id(我想)的变量名。
更新:
在OP添加实体类之后。
以Param
的身份传递时,您明确提到它是String
,这会导致IllegalArgumentException
,如实体所期望的HashSet<GameGenre>
。
所以方法签名应该是
Page<GameDTO> findAllOrderByTournamentCountAndGenreFilter(@Param("name") String name, @Param("genres") HashSet<GameGenre> genres, Pageable page);
,您应该将Param
传递为
GameGenre gameGenre = new GameGenre();
gameGenre.setGenreId(1301);
HashSet<GameGenre> genres = new HashSet<GameGenre>();
geners.add(gameGenre);
希望这会有所帮助。