I think this is just a limitation of Java-OO design. You need a parameterized way to pass predicates for the search, for example:
List<T> findByPredicate(List<Predicate> predicates, Class<T> returnType);
If the predicate class is something like this
class Predicate { String columnName; Operator operator; String value; }
Therefore, you can express "name =" John ", age> = 21, etc.
This is not an ideal solution, the code becomes less understandable for humans, you will need to convert predicates to database queries, and you need to execute a small number of types that are prone to runtime errors.
You can avoid wheel reuse with a library such as Spring Data. You donโt even need a common DAO, you just need to provide an interface method, for example
List<Person> findByName(String name);
and the implementation will be automatically generated when the application loads. Take a look at Spring Data JPA for more.
source share