使用 JPA 查询将集合与实体内的集合进行比较

问题描述

我正在尝试使用字符串集合对实体集合字段进行搜索。这是我目前所拥有的:

产品

@Entity(name = "product")
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "product_category",joinColumns = @JoinColumn(name = "product_id"),inverseJoinColumns = @JoinColumn(name = "category_name")
    )
    private Set<Category> categories;
}

类别

@Entity(name  = "category")
public class Category {
    @Id
    @Column(columnDeFinition = "VARCHAR(40)")
    private String name;

    public Category(String categoryName) {
        name = categoryName;
    }

    public Category() {
    }
}

过滤器集合示例:

Set<String> categories = new HashSet<Integer>(Arrays.asList("new","sale","bestseller"));

我还想传递与集合元素相同类型的对象集合,其中一个字段被分配了所需的值。但我认为这没有意义,因为我仍然只需要比较其中的一个领域:

Set<String> categories = new HashSet<Integer>(Arrays.asList(new Category("new"),new Category("sale"),new Category("bestseller")));

ProductRepository 中的方法

@Query("SELECT m FROM meet m WHERE (:categories IS EMPTY OR :categories IN m.categories")
Page<Meet> findAllByFilters(Pageable pageable,@Param("categories") Set<String> categories);

方法绝对不适合这种情况,因为我们假设集合是单个元素,而不是按元素比较每个集合。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)