I wonder if there is any elegant way, to filter on Employee entity following data model below.
So if i need to filter on "name", "hourlyRate" and "salary" i can't use jpa specification, i need write a raw sql
model
@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;
}
Type classes
@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]
CodePudding user response:
You can write a HQL query like the following:
from Employee e
where (type(e) = FullTimeEmployee and e.salary > 1000)
or (type(e) = PartTimeEmployee and e.hourlyRate > 100)