如何在 QueryDSL 中创建常量数值表达式?

发布于 2024-12-08 07:21:47 字数 1046 浏览 0 评论 0原文

我想用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别想她 2024-12-15 07:21:47

我们不久前从 Querydsl 中删除了 DSL Constant 类型。如果你真的想用第一种方式写它,那么你必须这样表达它:

Expressions.operation(Float.class, Ops.SUB, 
    Expressions.constant(0.12f), w.totalCost)

或者

NumberOperation.create(Float.class, Ops.SUB, 
    Expressions.constant(0.12f), w.totalCost)

如果你需要一个 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 :

Expressions.operation(Float.class, Ops.SUB, 
    Expressions.constant(0.12f), w.totalCost)

or

NumberOperation.create(Float.class, Ops.SUB, 
    Expressions.constant(0.12f), w.totalCost)

if you need a NumberExpression

抹茶夏天i‖ 2024-12-15 07:21:47

我前段时间遇到了同样的问题(需要常量 0),并且确实构建了自己的 ConstantNumberExpression 类。结果出人意料地简单:-)

一位同事也遇到了同样的问题,对于常数 1,所以我决定将其发布在这里。

private static class ConstantNumberExpression extends NumberExpression<Integer> {
    private static final long serialVersionUID = 1220768215234001828L;

    public ConstantNumberExpression(final int constant) {
        super(new ConstantImpl<>(constant));
    }

    @Override
    @Nullable
    public <R, C> R accept(final Visitor<R, C> v, @Nullable final C context) {
        return v.visit((Constant<Integer>) mixin, context);
    }
}

当然,这可以使用类型参数来更通用一些,但我们确实只需要它用于整数(实际上仅用于零和一)。

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.

private static class ConstantNumberExpression extends NumberExpression<Integer> {
    private static final long serialVersionUID = 1220768215234001828L;

    public ConstantNumberExpression(final int constant) {
        super(new ConstantImpl<>(constant));
    }

    @Override
    @Nullable
    public <R, C> R accept(final Visitor<R, C> v, @Nullable final C context) {
        return v.visit((Constant<Integer>) mixin, context);
    }
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文