我有三个实体,如下所示:
@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进行查询,如图所示:
My Spring数据管理存储库:
@Repository
public interface EmployeeRepository<T extends Employee> extends JpaRepository<T, Long> {
Page<T> findAll(Specification<T> specification, Pageable pageable);
}
我的样本控制器
@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执行查询,但是与salary和hourlyWage等其他参数不会发生相同的情况,我收到此异常:
java.lang.IllegalArgumentException:无法找到属性 此ManagedType [Employee]
上的给定名称[salary / hourlyWage]
我明白问题是在超类中查找字段并且它不存在,我想知道我的问题是否有解决方案?
问候。
答案 0 :(得分:4)
@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;
}