简体   繁体   English

如何使用JPA中的where子句从联接表中获取数据?

[英]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: 模板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;

The Problem im facing is, im very untrained when it comes down to use the JPA Interfaces. 当使用JPA接口时,我面临的问题是未经培训。
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? 我如何使用JPA接口(javax.persistence.criteria.Root,javax.persistence.Join等)将此查询构建为谓词? I'm sorry if im being unclear, i have to use this and im not used to using the JPA. 如果我不清楚我很抱歉,我必须使用它,而我不习惯使用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 一般而言,有关内部联接和HQL的更多详细信息: 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 JPA Criteria API 为连接表添加 where 子句 - Add where clause for joined table with JPA Criteria API 如何使用where子句更新spring数据JPA中的表 - how to update a table in spring data JPA using where clause 条件-联接表上的where子句 - Criteria - where clause on joined table 如何从 Spring 中的 REDIS 获取数据基于 where 子句中的多个参数启动,就像我们在 JPA 中作为 findByIdAndName 获取 - How to get Data from REDIS in Spring Boot based on multiple parameter in where clause like we get in JPA as findByIdAndName 使用where子句连接Spring Data JPA中的两个表实体 - Joining two table entities in Spring Data JPA with where clause Hibernate从联接表中获取数据 - Hibernate get data from joined table Spring JPA:从没有where子句的表中查询一列 - Spring JPA : Query one column from the table without where clause 如何通过JPA从联接的第二张表中检索特定行 - How to retrieve specific rows from joined second table through JPA 如何使用条件(where 子句)更新实体并在 spring 数据 jpa 中的方法响应中获取更新的实体 - How to update an entity with a condition(where clause) and get the updated entity in method response in spring data jpa 如何从 Spring Jpa 中的数据库中获取两个连接表? - How get two joined tables from database in Spring Jpa?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM