HQL加入集合元素

时间:2019-04-22 20:59:02

标签: java hibernate join collections hql

我正在尝试创建HQL,如果给定产品和提供者的价格存在,它将把我的产品与价格联系起来。

// The main entity
@Entity public class Product {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String name;

    @ManyToMany
    private Set<Provider> providers;
}

// Contained in Product as Set<Provider>
@Entity public class Provider {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String name;

    private String address;
}

// Each provider can have its own Price for given Product
@Entity public class Price {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @OneToOne
    private Product product;

    @OneToOne
    private Provider provider;

    private BigDecimal amount;
}

我想写一个HQL,它将返回一些SearchResultDTO,其中包含带有提供者和价格的产品。

要获得此价格,我需要将价格加入一组位于产品内部的提供者上,而我不知道该怎么做。

完成任务的本地SQL:

// The SQL
SELECT prod.name, prov.name, pr.amount
FROM product prod
LEFT OUTER JOIN products_providers pp ON pp.product_id = prod.id
LEFT OUTER JOIN provider prov ON prov.id = pp.providers_id
LEFT OUTER JOIN price pric ON (pric.product_id = prod.id AND pric.provider_id = prov.id);

我也乐于接受体系结构的提示,因为在将价格引入模型之后,我不确定我是否正确地将Providers嵌入到Product中。

0 个答案:

没有答案