簡體   English   中英

如何使用JPA中的where子句從聯接表中獲取數據?

[英]How to get data from joined table with where clause in JPA?

我有一個具有以下映射的實體:

@Entity
@Table(name = "template_product")
public class TemplateProductBean implements Serializable {
    private static final long serialVersionUID = -1821696115330320798L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "product_id")
    private Long productId;

    // bi-directional many-to-one association to
    // Template
    @ManyToOne
    @JoinColumn(name = "template_id")
    private TemplateBean template;

模板Bean如下所示:

@Entity
@Table(name = "template")
public class TemplateBean implements Serializable {

     private static final long serialVersionUID = 3752018564161042623L;

     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     @Column(name = "id")
     private Long id;

     @Column(name = "platform_id")
     private Long platformId;

     @Column(name = "template_name")
     private String templateName;

     // bi-directional many-to-one association to
     // TemplateProduct
     @OneToMany(mappedBy = "template")
     private List<TemplateProductBean > templateProductBean;

當使用JPA接口時,我面臨的問題是未經培訓。
我如何使用這些獲取以下查詢:

select template
            from Template template
            join TemplateProductBean templateProducts
            on template.templateId = templateProducts.template.templateId
            where templateProducts.productId in :templateProductIdList

我如何使用JPA接口(javax.persistence.criteria.Root,javax.persistence.Join等)將此查詢構建為謂詞? 如果我不清楚我很抱歉,我必須使用它,而我不習慣使用JPA。 謝謝

我認為這應該工作

select t from TemplateBean t inner join t.templateProductBean tpb where tpb.productId in :templateProductIdList

一般而言,有關內部聯接和HQL的更多詳細信息: 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM