简体   繁体   中英

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

i have an Entity with following mappings:

@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;

The Template Bean looks as following:

@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;

The Problem im facing is, im very untrained when it comes down to use the JPA Interfaces.
How can i use these to get the following query:

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

How can i use the JPA Interfaces( javax.persistence.criteria.Root, javax.persistence.Join etc.) to build this query as a Predicate? I'm sorry if im being unclear, i have to use this and im not used to using the JPA. Thanks

I think this should work

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

More details on inner joins and HQL in general: documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM