在Spring Data JPA中按类型查询

时间:2014-01-14 20:52:40

标签: java jpa spring-data spring-data-jpa

我是抽象类:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class A {
  ...
}

和几个扩展类,如:

@Entity
public class B extends A {
  ...
}

我也有第三个实体:

@Entity
public class C  {

  @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  private A objectA;
  ...
}

问题是,如何在C实体存储库中构建Spring Data JPA finder,以仅查询扩展A所需类型的对象?

1 个答案:

答案 0 :(得分:8)

您可以使用已定义“type”

的鉴别器值的名称
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@Table(name = "abc")
public abstract class Abc {
....

-------------------------------------------

@Query("select a from Abc a where type = ?1")
List<Abc> findByType(String typeValue);