Spring数据 - 按类型继承搜索

时间:2014-09-26 08:09:18

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

让我们假设我们有以下实体:

@Entity
@DiscriminatorColumn(name="Type")
@Inheritance(strategy=InheritanceType.Joined)
public abstract class Employee extends Persistable<Long> {

    private String firstName;
    private String lastName;

    //getters, setters
}

@Entity
@DiscriminatorValue("S")
public class SalariedEmployee extends Employee {
            //various fields
}

@Entity
@DiscriminatorValue("C")
public class ContractEmployee extends Employee {
            //various fields
}

@Entity
@DiscriminatorValue("H")
public class HourlyEmployee extends Employee {
            //various fields
}

Repository

public interface EmployeeRepository implements PagingAndSortingRepository<Employee, Long> {
}

我如何搜索查询可以说&#34;带给我所有不是HourlyEmployees&#34;的员工。显然我会要求对它们进行分页/排序,所以我可能不能findAll();并且如果我没有弄错的话,只删除页面集合中的条目。

1 个答案:

答案 0 :(得分:1)

请参阅JPQL类型运算符:

http://en.wikibooks.org/wiki/Java_Persistence/JPQL#JPQL_special_operators

JPA 2.0规范(第4.6.17.4节)给出了以下用法示例:

SELECT e
FROM Employee e
WHERE TYPE(e) IN (Exempt, Contractor)

SELECT e
FROM Employee e
WHERE TYPE(e) IN (:empType1, :empType2)

SELECT e
FROM Employee e
WHERE TYPE(e) IN :empTypes

SELECT TYPE(e)
FROM Employee e
WHERE TYPE(e) <> Exempt