简体   繁体   English

如何在字符串列上使用HIbernate标准进行比较,将其视为长列

[英]How to compare using HIbernate criteria on a string column treating it as a long column

I am working on a web project written in java that uses Hibernate for data access from a oracle database. 我正在使用Java编写的Web项目,该项目使用Hibernate从oracle数据库进行数据访问。

I have a column name serial in my database which is defined as VARCHAR(12) even though it contains only values that can be cast into long . 我的数据库中有一个列名称serial ,该列名称定义为VARCHAR(12)即使它只包含可以转换为long My intention is get all serials (treating them as numbers) between numbers fromNo and toNo . 我的意图是获取从fromNotoNo之间的所有序列号(将它们作为数字进行处理)。 I cannot use Restrictions.between because the serial column is not a number in DB. 我不能使用Restrictions.between因为serial列不是DB中的数字。 It is defined as String in the associated object as well. 在关联对象中也将其定义为String Right now I achieve the requirement (in an ugly way) by converting the number range to a list of strings and do a Restricitons.in on the column. 现在,我通过将数字范围转换为字符串列表并在该列上执行Restricitons.in (以丑陋的方式)来达到要求。

long fromNO = 10;
long toNo = 100;
List<String> listNos = null;
for (long k = fromNo; k <=toNO; k++) {
    listNos.add(k.toString());
}

Criteria criteria = getMyHibernateSession().createCriteria(MyObject.class);
criteria.add(Restrictions.in('serialNo',listNos));
List<MyObject> results = criteria.list();

Though I get the desired results, the problem happens when the toNo is not defined by the user and the tool need to get all the serial from fromNo 虽然我得到了预期的结果,但是当用户未定义toNo且工具需要从fromNo获取所有serialfromNo

My question is how can write a hibernate criteria where I can overcome all this problem, by specifying Criteria to treat serial column as long and not String ? 我的问题是,如何通过指定Criteria将serial列视为long而不是String来编写休眠标准,从而克服所有这些问题? (Some sort of casting process may be?) (可能是某种铸造过程?)

You can add new 'long' field in your entity annotated as 您可以在实体中添加新的“长”字段,其注释为

    @Formula("cast(serialNo as NUMBER(10,0))")
    private int lSerialNo;

Then use the new field lSerialNo in your Restrictions 然后在您的Restrictions使用新字段lSerialNo

Please try to use Restrictions.between. 请尝试使用Restrictions.between。 Try to parse the integer to string 尝试将整数解析为字符串

criteria.add(Restrictions.between('serialNo',String.valueOf(listNosFrom),String.valueOf(listNosTo)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Hibernate 中将长列转换为字符串的最佳方法 - Best way to convert long column to String in Hibernate 使用休眠条件将“投影”字段设置为休眠状态中的外键列 - Using Hibernate Criteria set Projection field as Foreign key column in the hibernate 比较两个字符串,其中一个使用Hibernate中的条件由空格分隔 - compare two string having one is separated by space using criteria in Hibernate 使用Hibernate,是否有一种方法可以避免在HQL / SQL / Criteria条件中对列名称使用字符串? - With Hibernate, is there a way to avoid using a String for column names in my HQL/SQL/Criteria conditions? 对Set列的Hibernate Criteria查询 - Hibernate Criteria query for a Set column 使用休眠条件查询CLOB列 - Querying CLOB column with hibernate criteria 在非主键列上使用Hibernate Criteria进行内部联接 - Inner joins using Hibernate Criteria on non-primary key column 在冬眠中使用条件对varchar类型的列进行编号和排序? - applying to number and order by for a column of varchar type in hibernate using criteria? 在Hibernate中如何在多对多关联中使用索引列? - How to use index column in many-to-many association in criteria in Hibernate? 休眠条件如何获取具有最大列值的行 - Hibernate criteria how to get rows with max column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM