gpt4 book ai didi

Java Hibernate java.lang.IllegalArgumentException : Parameter value did not match expected type

转载 作者:行者123 更新时间:2023-12-02 01:56:37 30 4
gpt4 key购买 nike

在游戏实体中我有:

private Set<GameGenre> genres = new HashSet<>();

我编写了自定义查询,该查询将按流派 ID 过滤游戏。当我在查询中对流派 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")

但是当我尝试传递 ids 列表时:

@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

游戏实体:

@Entity
@Table(name = "game")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "game")
public class Game implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;

@Column(name = "name")
private String name;

@Column(name = "active")
private Boolean active;

@Lob
@Column(name = "description")
private String description;

@Column(name = "cover_image")
private String coverImage;

@Column(name = "logo")
private String logo;

@Column(name = "background_image")
private String backgroundImage;

@Column(name = "featured_image")
private String featuredImage;

@Column(name = "character_image")
private String characterImage;

@Column(name = "small_cover_image")
private String smallCoverImage;

@ManyToMany
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JoinTable(name = "game_genres",
joinColumns = @JoinColumn(name="games_id", referencedColumnName="id"),
inverseJoinColumns = @JoinColumn(name="genres_id", referencedColumnName="id"))
private Set<GameGenre> genres = new HashSet<>();

最佳答案

您的查询应该是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);

希望这有帮助。

关于Java Hibernate java.lang.IllegalArgumentException : Parameter value did not match expected type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52202327/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com