java 在 hql 查询或条件中将字符串列转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30230893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:43:36  来源:igfitidea点击:

Casting the string column as int in hql query or criteria

javahibernatehql

提问by user4758229

Can anyone guide me where i am going wrong? Its giving zero result but in db values are existing as for condition appear in the below query.

谁能指导我哪里出错了?它给出零结果,但在 db 值中存在,因为条件出现在下面的查询中。

  Str = QueryImpl(from ArcZipCodeRange where cast(fromZip as int) >='12345' and cast(toZip as int)<='12345')
        arcZipCodeRangeList = 0

回答by FazoM

Are you sure your conditions are correct?

你确定你的条件正确吗?

cast(fromZip as int) >='12345' and cast(toZip as int)<='12345')

Would give all results with from> 12345 and to< 12345.

将给出from> 12345 和to< 12345 的所有结果。

Should it be other way around: from< 12345 and to> 12345?

应该是相反的: from< 12345 和to> 12345?

回答by Jens Schauder

you are casting fromZipand toZipto an int and then compare it with a String. That is asking for trouble.

您正在将fromZipandtoZip转换为 int,然后将其与 String 进行比较。那是自找麻烦。

Use the same data type on both sides of the comparison.

在比较的两边使用相同的数据类型。

Also as @Fazovskynotes, your condition seems to be the wrong way round.

同样正如@Fazovsky指出的那样,您的情况似乎是错误的。

回答by Afsun Khammadli

You can do this by Hibernate Criteriaeasily. For solution you must create your own Hibernate Formulafor these(fromZip and toZip). The following must be your pojo mapping.

你可以Hibernate Criteria很容易地做到这一点。对于解决方案,您必须Hibernate Formula为这些(fromZip 和 toZip)创建自己的解决方案。以下必须是您的 pojo 映射。

@Column
private String fromZip;

@Formula(value="to_number(fromZip)")
private double newfromZip;

@Column
private String toZip;

@Formula(value="to_number(toZip)")
private double newtoZip;

Following is your criteria for select:

以下是您的选择标准:

Criteria criteria = session.createCriteria(ArcZipCodeRange.class);
criteria.add(Restrictions.le("newfromZip", yourIntegerParameter));
criteria.add(Restrictions.ge("newtoZip", yourIntegerParameter));
List<ArcZipCodeRange> list = criteria.list();

I hope this will help you.

我希望这能帮到您。