如何在 QueryDSL 中创建常量数值表达式?
我想用 QueryDSL 创建这样的查询
update WorkMessage w set w.totalPrice = 0.12 - w.totalCost;
我尝试过这样
Expression<Float> priceExpr = Expressions.constant(0.12f);
new JPAUpdateClause(em, w)
.set(w.totalPrice , priceExpr.subtract(w.totalCost));
但这不起作用 - 表达式没有减法方法。
我是这样做的:
new JPAUpdateClause(em, w)
.set(w.totalPrice , w.totalCost.subtract(0.12f).negate());
但我想知道如何以第一种方式做到这一点。
//编辑
第二种方法不起作用:
JPAUpdateClause.toString说:
update WorkMessage workMessage
set workMessage.totalPrice = -(workMessage.totalCost - :a1)
但SQL结果是
update work_message set total_price=-total_cost-?
括号刚刚消失。我做错了什么吗?看起来这些:
w.totalCost.subtract(0.12f).negate()
w.totalCost.negate().subtract(0.12f)
具有相同的结果。
对于上述问题
w.totalCost.negate().add(0.12f)
有效。但我认为有一个错误。
I'd like to create query like this with QueryDSL
update WorkMessage w set w.totalPrice = 0.12 - w.totalCost;
I tried like this
Expression<Float> priceExpr = Expressions.constant(0.12f);
new JPAUpdateClause(em, w)
.set(w.totalPrice , priceExpr.subtract(w.totalCost));
But this doesn't work - Expression doesn't have subtract method.
I did it like this:
new JPAUpdateClause(em, w)
.set(w.totalPrice , w.totalCost.subtract(0.12f).negate());
but I'd like to know how to do it the first way.
//EDit
The second way don't work:
JPAUpdateClause.toString says:
update WorkMessage workMessage
set workMessage.totalPrice = -(workMessage.totalCost - :a1)
but the SQL result is
update work_message set total_price=-total_cost-?
Parentheses just dissapeared. Am I doing something wrong? It looks like theese:
w.totalCost.subtract(0.12f).negate()
w.totalCost.negate().subtract(0.12f)
have the same result.
For the above problem
w.totalCost.negate().add(0.12f)
works. But I think there is a bug.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们不久前从 Querydsl 中删除了 DSL Constant 类型。如果你真的想用第一种方式写它,那么你必须这样表达它:
或者
如果你需要一个 NumberExpression
We removed the DSL Constant types some time ago from Querydsl. If you really want to write it the first way, then you have to express it like this :
or
if you need a NumberExpression
我前段时间遇到了同样的问题(需要常量 0),并且确实构建了自己的 ConstantNumberExpression 类。结果出人意料地简单:-)
一位同事也遇到了同样的问题,对于常数 1,所以我决定将其发布在这里。
当然,这可以使用类型参数来更通用一些,但我们确实只需要它用于整数(实际上仅用于零和一)。
I had the same problem some time ago (needed the constant 0), and did build my own ConstantNumberExpression class. It turned out surprisingly easy :-)
A collegue just had the same problem, for the constant 1, so I decided to post it here.
Of course this could be done a bit more generic, using a type parameter, but we did need it only for Integer (actually only for zero and one).