[英]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.