spring-data-jpa JPA标准:如何过滤多个实体子类@继承(策略= InheritanceType.SINGLE_TABLE)

xuo3flqw  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(155)

我想知道是否有任何优雅的方式,过滤雇员实体以下的数据模型。
因此,如果我需要过滤“姓名”,“hourlyRate”和“薪金”,我不能使用jpa规范,我需要写一个原始的sql
模型化

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
   @Entity
   @DiscriminatorColumn(name = "EMP_TYPE")
        JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "EMP_TYPE", visible = true)
        @JsonSubTypes({
          @JsonSubTypes.Type(value = FullTimeEmployee .class, name = "F"),
          @JsonSubTypes.Type(value = PartTimeEmployee .class, name = "P"),

        })

    public class Employee {
     @Id
     @GeneratedValue
     private long id;
     private String name;

    }

类型类别

@Entity
@DiscriminatorValue("F")
public class FullTimeEmployee extends Employee {
  private int salary;
    .............
}

@Entity
@DiscriminatorValue("P")
public class PartTimeEmployee extends Employee {
  private int hourlyRate;
    .............
}

'SHOW TABLES'
[EMPLOYEE, PUBLIC]
'SHOW COLUMNS from EMPLOYEE'
[EMP_TYPE, VARCHAR(31), NO, , NULL]
[ID, BIGINT(19), NO, PRI, NULL]
[NAME, VARCHAR(255), YES, , NULL]
[HOURLYRATE, INTEGER(10), YES, , NULL]
[SALARY, INTEGER(10), YES, , NULL]
rqmkfv5c

rqmkfv5c1#

您可以编写如下所示的HQL查询:

from Employee e 
where (type(e) = FullTimeEmployee and e.salary > 1000) 
   or (type(e) = PartTimeEmployee and e.hourlyRate > 100)

相关问题