- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何用 PathBuilder 替换以下使用生成的 Q* 类和 java 反射的方法?
// member vars:
T operand; // can be a BigDecimal or a String
String tableName;
String fieldName;
String methodName;
public Predicate asPredicate()
{
Class<?> tableClazz = Class.forName("foo.bar.database.model.Q"+ WordUtils.capitalize(tableName));
Object tableObj = tableClazz.getConstructor(String.class).newInstance(tableName +"1000");
Field colField = tableClazz.getDeclaredField(fieldName);
Object colObj = colField.get(tableObj);
Class classParam = Object.class;
if(methodName.matches(".*like"){
classParam = String.class;
}
// method name is one of eq, ne, like...
Method m = colObj.getClass().getMethod(methodName, classParam );
return (Predicate) m.invoke(colObj, operand);
}
这很好用,但建议我在回答我的其他问题时使用 PathBuilder https://stackoverflow.com/questions/15269845/querydsl-extract-table-name-from-predicate-booleanexpression-object )这也会删除尴尬的 newInstance(tableName +"1000")。
PathBuilder<?> entityPath = new PathBuilder("foo.bar.database.model.Q"+ WordUtils.capitalize(tableName), "entity"); // what does the second param stand for?
PathBuilder relation = entityPath.get(fieldName);
// ???
两个问题:1)我现在可以在关系上调用 eq() 或 ne() 但不能调用 like()、notLike()2) 如何获取 colObj 以便我可以使用 java 反射 colObj.getClass().getMethod(...)
解决方案:感谢 Timo 的回答,除了两个 instanceof 条件之外,我完全放弃了反射,现在使用此代码:
tableClazz = Class.forName("foo.bar.database.model."+ WordUtils.capitalize(tableName));
PathBuilder<?> entityPath = new PathBuilder(tableClazz, tableName +"1000");
Predicate predicate = null;
if(operand instanceof String){
StringPath path = entityPath.getString(fieldName);
switch(type){
case EQ:
predicate = path.eq((String) operand);
case CONTAINS:
predicate = path.like("%" + operand +"%");
break;
// snip BEGINS WITH, ENDS WITH
}
}else if(operand instanceof BigDecimal){
assert(type.equals(Type.EQ));
NumberPath<BigDecimal> path = entityPath.getNumber(fieldName, BigDecimal.class);
predicate = path.eq((BigDecimal) operand);
}
if(negation){
return predicate.not();
}
return predicate;
最佳答案
你应该这样使用它
// entityClass is the entity type, not the Q-type
Class<?> entityClass = Class.forName(...)
// "entity" is the variable name of the path
PathBuilder<?> entityPath = new PathBuilder(entityClass, "entity");
// use getString to get a String path
Predicate predicate = entityPath.getString("property").like("a%");
关于QueryDSL:从 PathBuilder 生成谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15273542/
我正在开发一个包含 TreeViewer 的 RCP 应用程序,我想在其上激活多个应与“OR”谓词配合使用的过滤器,例如: A |--B |--|--redColor |--|--blueColor
我的问题是关于 enable_if通常标准库中的谓词,但我将在迭代器类型的上下文中构建它,因为这是我目前遇到此问题的地方。 我有一个自定义迭代器类型 It , 这样 std::iterator_tra
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
我需要使用自然数为 2 的幂创建一个 Prolog 谓词。 自然数是:0、s(0)、s(s(0)) 等等。 例如: ?- pow2(s(0),P). P = s(s(0)); false. ?- po
我正在尝试创建一个 NSPredicate 来查找在特定日期范围内包含“ session ”的“项目”。我一开始尝试过这个: [NSPredicate predicateWithFormat:@"AN
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
在JOOQ中,我可以编写如下SQL这样的代码吗? 我不知道如何编写具有多个字段的in谓词。 select some_value from t1 where (t1.id1, t1.id2) i
我正在用 PROLOG 编写一个数独求解器。我希望求解器能够处理所有可能大小的数独,因此我自然需要构造采用可变数量参数的谓词。 (例如在数独中构建“ block ”。) 如何构造或模拟具有可变数量参数
我有一个包含一些 id 的列表,我希望我的 ebean 查询排除这些 id。所以基本上我需要在 ebean 中使用“not in”谓词,就像 sql 一样,但遗憾的是找不到。还有其他方法可以实现这一目
我正在构建一个通用接口(interface)以从类中公开选定的字符串属性,然后我想在每个字段中搜索文本,以检查它是否匹配。 这是我的 IFieldExposer接口(interface): using
我将 Spring Boot 与 Spring JPA 和 Specification Executor 结合使用。我的规范/谓词组合成功地在我的类中搜索了简单的属性。但是,我在搜索其中的对象时遇到了
如果下面的 last_name 为 NULL,它会跳过该列的 WHERE 比较以提高性能吗? AND (last_name IS NULL OR sp.last_name LIKE CONCAT('%
出于好奇:如果我有一个接受多个参数(通常为 1 或 2)并返回 3 个值中的 1 个(而不是 bool 值 true 或 false)的类运算符(或函数等),它是否仍应被调用谓词?还是模糊逻辑的特例?
是否可以创建一个采用装箱值类型并返回该值类型是否等于该类型默认值的方法? 所以我想创建一个具有以下签名的方法: bool IsDefault(object boxedValueType); 注意:当
let selectedConsoles = ["Xbox", "Playstation 4"] let players = realm.objects(Person).filter("console
我正在尝试根据用户搜索文本过滤来自核心数据的结果,但效果很好。我正在努力做到有几个关键术语可以返回特定结果。 我有一个Colour 实体,它与另一个实体ProjectColour 具有对多 关系。 P
std::vector lines; typedef std::vector::iterator iterator_t; iterator_t eventLine = std::find_if(lin
我想在一个列表中找到一个元素的索引,该列表匹配某个谓词,有没有比以下更好的方法: var index = list.IndexOf(list.Find(predicate)); ? 最佳答案 你在找
我正在使用缺少 findall 的高阶 Prolog 变体. 还有一个关于实现我们自己的问题 findall这里:Getting list of solutions in Prolog . 低效的实现
我正在使用 Breeze 过滤客户端请求的数据。我的代码看起来有点像这样: 客户端 - 创建过滤谓词 var predicates = []; var criteriaPredicate = null
我是一名优秀的程序员,十分优秀!