多对多显式关系的 HQL 查询
HQL query for Many to Many Explict relationship
我正在尝试为以下模型编写 hql。
Product.java
@Entity public class Product implements Serializable {
private static final long serialVersionUID = -3532377236419382983L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int productId;
@NotEmpty(message = "The product name must not be empty")
private String productName;
private String productCategory;
private String productDescription;
@ManyToMany
@JsonIgnore @JoinTable(name="PRODUCT_SUBCATEGORY",
joinColumns={@JoinColumn(name="productId")},
inverseJoinColumns={@JoinColumn(name="subCategoryId")})
private Set<SubCategory> subCategory = new HashSet<SubCategory>(); //getter setter
SubCategory.java
@Entity
public class SubCategory implements Serializable {
private static final long serialVersionUID = 7750738516036520962L;
@Expose(serialize = true, deserialize = true)
@Id
@GeneratedValue
private Integer subCategoryId;
@Expose(serialize = true, deserialize = true)
@NotEmpty(message = "The subcategory name must not be empty")
@Size(min = 3, max = 20, message = "Minimum 3 to 20 characters allowed")
private String subCategoryName;
@Expose(serialize = false, deserialize = false)
@ManyToOne
@JoinColumn(name="categoryId")
private Category category;
//getter setter
我想根据子类别table的子类别名称进行查询,以通过PRODUCT_SUBCATEGORYtable获取产品数据。示例查询如下。
select * from product where productid in(
select psc.productid from
subcategory sc
inner join product_subcategory psc
on sc.subcategoryid=psc.subcategoryid
where sc.subcategoryname like 'men ware%');
如何在 hql 中实现上述 sql。
谢谢。
select distinct p from Product p
join p.subCategory sc
where sc.subCategoryName like :name
我正在尝试为以下模型编写 hql。
Product.java
@Entity public class Product implements Serializable {
private static final long serialVersionUID = -3532377236419382983L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int productId;
@NotEmpty(message = "The product name must not be empty")
private String productName;
private String productCategory;
private String productDescription;
@ManyToMany
@JsonIgnore @JoinTable(name="PRODUCT_SUBCATEGORY",
joinColumns={@JoinColumn(name="productId")},
inverseJoinColumns={@JoinColumn(name="subCategoryId")})
private Set<SubCategory> subCategory = new HashSet<SubCategory>(); //getter setter
SubCategory.java
@Entity
public class SubCategory implements Serializable {
private static final long serialVersionUID = 7750738516036520962L;
@Expose(serialize = true, deserialize = true)
@Id
@GeneratedValue
private Integer subCategoryId;
@Expose(serialize = true, deserialize = true)
@NotEmpty(message = "The subcategory name must not be empty")
@Size(min = 3, max = 20, message = "Minimum 3 to 20 characters allowed")
private String subCategoryName;
@Expose(serialize = false, deserialize = false)
@ManyToOne
@JoinColumn(name="categoryId")
private Category category;
//getter setter
我想根据子类别table的子类别名称进行查询,以通过PRODUCT_SUBCATEGORYtable获取产品数据。示例查询如下。
select * from product where productid in(
select psc.productid from
subcategory sc
inner join product_subcategory psc
on sc.subcategoryid=psc.subcategoryid
where sc.subcategoryname like 'men ware%');
如何在 hql 中实现上述 sql。
谢谢。
select distinct p from Product p
join p.subCategory sc
where sc.subCategoryName like :name