Question

If I construct a predicate foo.bar=1234 like this

PathBuilder<?> entityPath = new PathBuilder("foo");
NumberPath<BigDecimal> path = entityPath.getNumber("bar", BigDecimal.class);
Predicate predicate = path.eq(BigDecimal.valueOf(1234));

How do I find later the argument value (1234)?

My attempt so far:

Path<?> path = (Path<?>) predicate.accept(PathExtractor.DEFAULT, null);
PathMetadata<?> md = path.getMetadata();

if(md.getExpression().toString().equals("bar")) {
   Object val = md.getPathType().VARIABLE;    // probably already a wrong approach...
   if(val instanceof BigDecimal) {
   // doesn't work
   }
}

Update, why I need this: Our Web Application allows users to create custom DB search queries, these can be saved/loaded to/from a database (using JAXB). Each query consists of one or more constraints which correspond to QueryDSL Predicates. The part of application that does the search itself has to look into the predicates among others to determine which DB tables are used to form the JOINs etc.

Was it helpful?

Solution

foo.bar=1234 is a Operation, foo and foo.bar are Path instances and 1234 is a Constant.

You can extract the constant for 1234 by casting the predicate to Operation

Constant constant = (Constant)((Operation)predicate).getArg(1);

What is your use case for this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top