Есть две энтити:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
public class QuestionEntity extends AbstractEntity {
@Type(type = "text")
private String text;
@Builder.Default
@ToString.Exclude
@EqualsAndHashCode.Exclude
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "questionCategoryRelation",
joinColumns = { @JoinColumn(name = "questionId") },
inverseJoinColumns = { @JoinColumn(name = "categoryId") }
)
Set<QuestionCategoryEntity> categories = new HashSet<>();
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
public class QuestionCategoryEntity extends AbstractEntity {
@Column(name = "name", nullable = false, updatable = false, unique = true)
private String name;
@EqualsAndHashCode.Exclude
private String color;
@Builder.Default
@ToString.Exclude
@EqualsAndHashCode.Exclude
@ManyToMany(mappedBy = "categories", fetch = FetchType.LAZY)
private Set<QuestionEntity> questions = new HashSet<>();
}
Есть репозиторий:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
@Repository
public interface QuestionRepository extends JpaRepository<QuestionEntity, Long> {
@Query(value="SELECT q FROM QuestionEntity q ORDER BY function('RAND')")
Optional<QuestionEntity> findTopRandomQuestion();
@Query(value="SELECT q FROM QuestionEntity q WHERE q.categories IN :categories ORDER BY function('RAND')")
List<QuestionEntity> findTopByCategoriesIn(@Param("categories") Set<QuestionCategoryEntity> categories);
}
Мне надо случайным образом выбрать вопрос у которого есть некие категории.
Но при выполнении получаю ошибку:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [QuestionCategoryEntity(name=Maven, color=null)] did not match expected type [java.util.Set (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [QuestionCategoryEntity(name=Maven, color=null)] did not match expected type [java.util.Set (n/a)]] with root cause java.lang.IllegalArgumentException: Parameter value [QuestionCategoryEntity(name=Maven, color=null)] did not match expected type [java.util.Set (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.QueryParameterBindingsImpl.expandListValuedParameters(QueryParameterBindingsImpl.java:636)
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1536)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)
at org.hibernate.query.Query.getResultList(Query.java:135)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:129)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:91)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:136)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:125)
Что не так?