Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA Specification Inheritance

I've three entities as shown below:

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

And i trying query using Spring Data Specification, as shown:

My Spring data managed repository:

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

And my sample 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;
    }
}

I can execute the query with the id, but the same does not occur with the other parameters like salary and hourlyWage, i receive this exception:

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

I understand that the problem is to look for the field in the superclass and it does not exist, I would like to know if there is a solution to my problem?

Regards.

like image 912
Joao Moreno Avatar asked Jan 30 '18 19:01

Joao Moreno


1 Answers

As @JBNizet tip to use 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;
}
like image 195
Joao Moreno Avatar answered Oct 30 '22 15:10

Joao Moreno