简体   繁体   English

QueryDSL:如何从PathMetadata对象获取参数

[英]QueryDSL: how to get an argument from a PathMetadata object

If I construct a predicate foo.bar=1234 like this 如果我像这样构造谓词foo.bar=1234

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)? 以后如何查找参数值(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). 更新,为什么我需要这样做:我们的Web应用程序允许用户创建自定义的数据库搜索查询,这些查询可以保存(或使用JAXB加载到数据库中)。 Each query consists of one or more constraints which correspond to QueryDSL Predicates. 每个查询都包含一个或多个与QueryDSL谓词相对应的约束。 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. 搜索本身的应用程序部分必须仔细研究谓词,以确定哪些DB表用于形成JOIN等。

foo.bar=1234 is a Operation, foo and foo.bar are Path instances and 1234 is a Constant. foo.bar=1234是一个操作, foofoo.bar是Path实例,而1234是一个常量。

You can extract the constant for 1234 by casting the predicate to Operation 您可以通过将谓词强制转换为Operation来提取1234的常量

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

What is your use case for this? 您的用例是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM