首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多对多显式关系HQL查询

问多对多显式关系HQL查询
EN

Stack Overflow用户
提问于 2017-02-26 20:53:41
回答 1查看 29关注 0票数 0

我正在尝试为下面的模型编写hql。

Product.java

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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

我想根据子类别表的子类别名称进行查询,通过PRODUCT_SUBCATEGORY表获取产品数据。示例查询如下所示。

代码语言:javascript
复制
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。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-26 22:17:25

代码语言:javascript
复制
select distinct p from Product p 
join p.subCategory sc 
where sc.subCategoryName like :name
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42468774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档