Skip to content
Advertisement

What is Querydsl PathBuilder String parameter ‘variable’ used for

I’m working with Querydsl in my Spring Boot API for making some complex data filtering and when i declare the PathBuilder variable i’ve seen that first you have to pass statically your class like YourClass.class and then a string variable for the constructor as shown below:

    PathBuilder<Plan> entityPath = new PathBuilder<>(Plan.class, "plan");

So i was wondering why is necessary this string parameter and also why its name has to be the same as my class name, because when i delete it or modified it, the entityPath doesn’t work.

Advertisement

Answer

A PathExpression (which PathBuilder) is by definition represents a path variable. In your case, the path ending up in your query would be plan. Consecutively, you can build on to this path and create various path types. For example property retrieval: entityPath.get("someProperty") will return a path expression that represents plan.somePropety. So you can’t create PathExpressions without an alias, because it would be essentially meaningless.

Then why this alias is not always inferred from the Path types, well simply put: because it is not related. The underlying entities used may have different naming strategies, you may want to mix up different path variables of the same type, you might reference a node that has a particular alias by itself.

The alias doesn’t have to be equivalent to the simple classname, it has to be equivalent to the alias it references from any of the from/join clauses in the query.

Advertisement