I'm having an entity hierarchy like this. Besides some common properties, some properties are shared only by a few subtypes:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Person {
private String firstName;
private String lastName
... further properties, getters and setters...
}
@Entity
public class Employee extends Person {
private String salary;
... further properties, getters and setters...
}
@Entity
public class BoardMember extends Person {
private String salary;
... further properties, getters and setters...
}
@Entity
public class ExternalMember extends Person {
private String clearanceLevel;
... further properties, getters and setters...
}
@Repository
public interface PersonRepository extends JpaRepository<Person, Long>, QuerydslPredicateExecutor<Person> {
}
Using QueryDSL I'm trying to search for Persons depending on dynamic filter criterias like this:
@Service
@Transactional
public class PersonService {
@Autowired
PersonRepository personRepository;
public Page<Person> search(String firstName, String salary) {
var searchCriterias = new BooleanBuilder();
if (firstName != null) {
searchCriterias.and(QPerson.firstName.eq(firstName));
}
if (salary != null) {
searchCriterias.andAnyOf(
QPerson.person.as(QEmployee.class).salary.eq(salary),
QPerson.person.as(QBoardMember.class).salary.eq(salary),
);
}
personRepository.findAll(searchCriterias);
}
}
This doesn't seem to be the right way, however, I'm getting a lot of errors like "salary" not a member of Person.
What are the various ways to deal with searches for hierarchical entities? I'd prefer QueryDSL for type-safety, but solutions using Spring Data Specification would be fine as well.
edit: The search criterias could get very complex using 15+ different search criterias. So I need a programmatic approach to formulate them.