gpt4 book ai didi

java - Spring Data JPA 规范继承

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:01:52 24 4
gpt4 key购买 nike

我有如下所示的三个实体:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type")
public abstract class Employee {
@Id
protected Long id;
...
}

@Entity
public class FullTimeEmployee extends Employee {
protected Integer salary;
...
}

@Entity
public class PartTimeEmployee extends Employee {
protected Float hourlyWage;
}

我尝试使用 Spring Data Specification 进行查询,如图所示:

我的 Spring 数据管理存储库:

@Repository
public interface EmployeeRepository<T extends Employee> extends JpaRepository<T, Long> {
Page<T> findAll(Specification<T> specification, Pageable pageable);
}

还有我的示例 Controller

@Controller
public class MyController {
private final EmployeeRepository employeeRepository;

@Autowired
public MyController(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}

@GetMapping("/")
public ModelAndView queryEmployeeExample(EmployeeQueryDTO query) {
Page response = this.employeeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
ArrayList<Predicate> predicates = new ArrayList<>();
if(query.getId() != null) {
predicates.add(
criteriaBuilder.equal(root.get("id"), query.getId())
);
}
if(query.getSalary() != null) {
predicates.add(
criteriaBuilder.equal(root.get("salary"), query.getSalary())
);
}
if(query.getHourlyWage() != null) {
predicates.add(
criteriaBuilder.equal(root.get("hourlyWage"), query.getHourlyWage())
);
}
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
}, new PageRequest(0, 100));

return new ModelAndView("index")
.addObject("result", response);
}

@Getter
@Setter
static class EmployeeQueryDTO {
private Long id;
private Integer salary;
private Float hourlyWage;
}
}

我可以使用 id 执行查询,但对于薪水和 hourlyWage 等其他参数不会发生同样的情况,我收到此异常:

java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [salary/hourlyWage] on this ManagedType [Employee]

我了解到问题是在父类(super class)中查找该字段不存在,我想知道是否有解决我的问题的方法?

问候。

最佳答案

作为@JBNizet 提示使用CriteriaBuilder.treat():

@GetMapping("/")
public ModelAndView queryEmployeeExample(EmployeeQueryDTO query) {
Page response = this.employeeRepository.findAll((root,
criteriaQuery,
criteriaBuilder) -> {

ArrayList<Predicate> predicates = new ArrayList<>();

if(query.getId() != null) {
predicates.add(
criteriaBuilder.equal(
root.get("id"),
query.getId()
)
);
}
if(query.getSalary() != null) {
//Downcast Root object to the specified type.
Root<FullTimeEmployee> rootFullTImeEmployee = criteriaBuilder
.treat(root, FullTimeEmployee.class);
predicates.add(
criteriaBuilder.equal(
rootFullTImeEmployee.get("salary"),
query.getSalary()
)
);
}
if(query.getHourlyWage() != null) {
//Downcast Root object to the specified type.
Root<PartTimeEmployee> rootPartTimeEmployee = criteriaBuilder
.treat(root, PartTimeEmployee.class);
predicates.add(
criteriaBuilder.equal(
rootPartTimeEmployee.get("hourlyWage"),
query.getHourlyWage()
)
);
}
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));},
new PageRequest(0, 100));

return new ModelAndView("index")
.addObject("result", response);
}

@Getter
@Setter
static class EmployeeQueryDTO {
private Long id;
private Integer salary;
private Float hourlyWage;
}

关于java - Spring Data JPA 规范继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48529445/

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