gpt4 book ai didi

java - PagingAndSortingRepository 查询返回与一组实体匹配的实体

转载 作者:行者123 更新时间:2023-11-30 07:50:44 25 4
gpt4 key购买 nike

我正在将 JPA 与 Hibernate 结合使用。我有这个实体:

package com.test.model.entity;

@Entity
public class Article {
@ManyToMany(fetch = LAZY, cascade = DETACH)
private Set<Tag> tags;
}

我可以做类似的事情来获取给定集合中至少有一个标签匹配的所有文章吗?

public interface ArticleRepository extends PagingAndSortingRepository<Article, Long> {

Page<Article> findAllByTags(Set<Tag> tags, Pageable pageabled);
}

否则,我如何使用分页来获取它?

或者,我可以使用 @Query 并仍然保留分页吗?

更新

对我来说罗伯特的 answer返回

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: : near line 1, column 71 [SELECT a FROM com.test.model.entity.Article a WHERE a.tags = ANY (:tagsToContain)]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
at com.sun.proxy.$Proxy92.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86)
... 55 more

最佳答案

您可以组合@QueryPageable。尝试:

@Query("select a from Article a where a.tags = any (:tagsToContain)")
Page<Article> findAllByTags(Set<Tag> tagsToContain, Pageable pageabled);

关于java - PagingAndSortingRepository 查询返回与一组实体匹配的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33379310/

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