gpt4 book ai didi

java - Spring Data - 按类型进行继承搜索

转载 作者:行者123 更新时间:2023-11-30 03:44:37 25 4
gpt4 key购买 nike

假设我们有以下实体:

@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
}

还有一个存储库

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

我如何搜索执行查询,比如“将所有非小时员工的员工带给我”。显然,我需要对它们进行分页/排序,因此我可能无法执行 findAll(); 操作,如果我没有记错的话,只需从 Page 集合中删除条目即可。

最佳答案

查看 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

关于java - Spring Data - 按类型进行继承搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26054920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com