Java条件限制列总和低于值

编程入门 行业动态 更新时间:2024-10-17 09:45:16
本文介绍了Java条件限制列总和低于值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,我有一个关于将简单的SQL语句转换为Java休眠限制的问题.

Hi I've a question about converting a simple SQL statement into a java hibernate restriction.

SQL:

SELECT a + b FROM col WHERE a + b < 10

当我尝试将其转换为条件时,我得到:

When I try to convert this to a criteria I get:

Criteria criteria = createCriteria(); criteria.createAlias("col","col").add(Restrictions.lt("a+b",10));

但是似乎字符串"a+b"不被接受.我该如何写我的限制?

But it seems like the string "a+b" is not accepted. How do I have to write my restriction?

推荐答案

很可能是因为Hibernate试图将 a + b 解析为您实体的属性,并且您获得了

It's likely because Hibernate is trying to resolve a+b as your entity's property, and you get

org.hibernate.QueryException: could not resolve property: a+b

几乎没有解决方案:

第一个:sql限制:

//Hibernate 4.x criteria.add(Restrictions.sqlRestriction("col_a+col_b < ?", Integer.valueOf(10), org.hibernate.type.StandardBasicTypes.INTEGER); // Hibernate 3.x criteria.add(Restrictions.sqlRestriction("col_a+col_b < ?", Integer.valueOf(10), org.hibernate.Hibernate.INTEGER);

有一个问题,您必须使用列名而不是属性名.

There is one issue that you must use column names instead of property names.

第二名:公式:

使用 @Formula 注释将虚拟列添加到您的实体:

Add to your entity a virtual column using @Formula annotation:

@org.hibernate.annotations.Formula("a + b") private int aSumB;

并将其用作标准属性:

criteria.add(Restrictions.lt("aSumB", Integer.valueOf(10));

第三个自定义条件:

如果您经常使用它,请考虑通过实现 org.hibernate.criterion.Criterion

If you are using it a lot consider to create custom criteria by implementing org.hibernate.criterion.Criterion

更多推荐

Java条件限制列总和低于值

本文发布于:2023-11-21 23:33:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615109.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:总和   条件   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!