铸造字符串列以int HQL查询或标准

问题描述 投票:4回答:3

任何人都可以指导我我要去的地方错了吗?它使零的结果,但在分贝值成为这次的情况会出现在下面的查询。

  Str = QueryImpl(from ArcZipCodeRange where cast(fromZip as int) >='12345' and cast(toZip as int)<='12345')
        arcZipCodeRangeList = 0
java hibernate hql
3个回答
6
投票

你确定你的条件是正确的?

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

将给人以from> 12345 to <12345的所有结果。

它应该是周围的其他方式:from <12345和to> 12345?


2
投票

您是铸造fromZiptoZip为int然后将它与一个字符串进行比较。这是自找麻烦。

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

另外,作为@Fazovsky笔记,你的条件似乎是南辕北辙。


2
投票

您可以通过Hibernate Criteria很容易地做到这一点。对于解决方案,你必须创建自己的这些(fromZip和toZip)Hibernate Formula。下面一定是你的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;

以下是你的选择标准:

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

我希望这能帮到您。

© www.soinside.com 2019 - 2024. All rights reserved.