Getting dynamic property predicate by property name with queryDSL

querydsl, spring, spring-data, spring-data-jpa

Solution

To create a String typed property just use the following snippet

new StringPath(p, propertyName)

which can then be used like this to create a Predicate instance

new StringPath(p, propertyName).like("tom")

Problem

I using Query DSL generated entity EntitySerializer in order to query JPA entities using QueryDSL (integrated with SpringData). I’m receiving from the client property names and I want to create predicates for the properties that can be added (AND) to additional predicats. I don’t know how to get from the EntitySerializer the predicate/Path that matches my property name. For example, let’s say we have a Person entity (with auto generated QPerson class) with a “name” property that I want to filter on (at the end I want to create a generic method). Here is the generic method: ``` Public Predicat getPredicatByPropertyName(String propertyName) { QPerson p = QPerson.person; person.getPredicat(“propertyName”).like(“tom”); } ```

Original source

Related problems