【问题标题】:Query by type in Spring Data JPA在 Spring Data JPA 中按类型查询
【发布时间】:2014-02-03 02:48:06
【问题描述】:

我有抽象类:

@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 所需类型的对象?

【问题讨论】:

  • 为您的鉴别器添加只读属性或尝试select s from Sample s where TYPE(s) = :type
  • 我首先询问的是 spring-data,其次我需要查询的不是 type(c) 而是 type (c.objectA)

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


【解决方案1】:

您可以使用您定义“类型”的鉴别器值的名称

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 2019-07-10
    • 2015-01-30
    • 1970-01-01
    相关资源
    最近更新 更多