如何对整数字段执行 Restrictions.like
我在数据库(Postgresql)和我的休眠映射文件中有一个整数字段,我想在类似操作中使用它(例如Restrictions.like(Bean.fieldname,'123')
)。
对于没有显式类型转换 select * from table where text(myint) like '1%'
的整数,数据库不支持 like
。理想情况下,我希望将数据库字段类型和 Hibernate 属性类型保留为整数,而不必加载数据库中的所有字段以在 Java 代码中进行迭代。
干杯:)
I have an integer field in the DB (Postgresql) and my hibernate mapping file that I want to use in a like operation (e.g. Restrictions.like(Bean.fieldname,'123')
).
The database does not support like
for integer without explicit type casting select * from table where text(myint) like '1%'
. Ideally, I'd like to keep the DB field type and Hibernate property type as integer
s and not have to load all the fields from the DB to iterate through in the Java code.
cheers :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果该值确实是一个数字,我只需将其限制在一个范围内 - 例如大于或等于 100 且小于 200。我不会认为您真的想要“所有以 1" 开头的数字 - 这表明 1 和 10000 相似,而 1 和 2 完全不同。数字中的信息几乎总是与其大小相关,而不是其十进制表示形式中的数字。
If the value really is a number, I'd just restrict it to a range - e.g. greater than or equal to 100 and less than 200. I wouldn't have thought you'd really want "all numbers starting with 1" - that suggests that 1 and 10000 are similar, whereas 1 and 2 are totally different. The information in a number should almost always relate to its magnitude, not the digits from its decimal representation.
为什么需要一个“LIKE”?这是一个非常奇怪的比较,这也是它不是整数运算符的原因。
您可以将数据库中的值转换为 text/varchar,但除非您也创建特殊索引,否则会降低性能。
Why do you need a LIKE? It's a very strange comparison, that's also why it's not an integer operator.
You could cast the value in the database to text/varchar, but you will kill performance unless you create a special index as well.
Restrictions.sqlRestriction("CAST({alias}.myint AS CHAR) 像?", "%1%", Hibernate.STRING));
Restrictions.sqlRestriction("CAST({alias}.myint AS CHAR) like ?", "%1%", Hibernate.STRING));