如何过滤实体B加入JPA

时间:2018-06-28 23:40:27

标签: jpa jpql

  • 我有一个Entity_AOneToManyEntity_B关系。

  • Entity_B带有一个标志,指示它是否处于活动状态。

如何选择加入实体A并仅选择激活的实体B?

1 个答案:

答案 0 :(得分:0)

JPA Query:Documention

SELECT DISTINCT entA FROM Entity_A entA JOIN FETCH entA.entityBSet as entB WHERE entB.active = true

更新 -尝试@where

@Entity
public class Entity_A {
  @Id
  @Column(name = "ID")
  private Integer id;

  @OneToMany(mappedBy = "entityA")
  @Where("active = true")
  private Set<Entity_B> entityBSet;
}

@Entity
public class Entity_B {
  @Id
  @Column(name = "ID")
  private Integer id;

  @Id
  @Column(name = "ACTIVE")
  private Integer active;

  @ManyToOne
  private Entity_A entityA;
}