我有一个产品,可以有多个类别,产品和类别是许多相关的。告诉我如何通过从数组中选择至少有一个类别的产品来实现查询?
尝试-
@Query (value = "SELECT * FROM Product p join p.categories c where c.name = ANY (: categories)", nativeQuery = true)
Page <Product> findProductsByCategories (@Param ("categories") String [] categories, Pageable pageable);
但这个选项不起作用。错误- You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'x'ACED0005757200135B4C6A6176612E6C616E672E537472696E673BADD256E7E91D7B4702000 ...' at line 1
实体-
public class Product implements Serializable {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String description;
private LocalDateTime data;
private int mainValume;
private int mainCost;
@ManyToMany (fetch = FetchType.LAZY, targetEntity = Category.class)
@JoinTable (
name = "product_has_category",
joinColumns = {
@JoinColumn (name = "product_id")},
inverseJoinColumns = {
@JoinColumn (name = "category_id")})
private List <Category> categories = new ArrayList <> ();
}
和
public class Category implements Serializable {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private long id;
private String name;
@ManyToMany (mappedBy = "categories", targetEntity = Product.class, fetch = FetchType.LAZY)
private List <Product> products = new ArrayList <> ();
}
暂无答案!
目前还没有任何答案,快来回答吧!