Hibernate 的 <= 出现问题
- 问题模拟
select new map( e.name as name , e.salary as salary) from Emplpyee e where e.salary <= :salary
设置值后查询不出结果,足足查了一个来小时
后来在一步一步执行sql的时候发现问题
- 问题原因
sql
select e.name as name,e.salary as salary from Employee e where e.salary <= ?
对于hibernate组成的sql最终为
select e.name as name,e.salary as salary from Employee e where e.salary <= '2000'
可能有些朋友已经看出问题来了,就是包裹2000的''
它使得2000变成了一个字符串,然后比较的是字典序
然后发现我的数据库的salary字段以及实体类的salary字段都设置成了字符串类型
- 解决方案
数据库的salary字段以及实体类的salary字段都设置为数字类型(NUMBER,BigDecimal)
之后的sql,就少了那个'',查询成功
select e.name as name,e.salary as salary from Employee e where e.salary <= 2000
本博客文章主要供博主学习交流用,所有描述、代码无法保证准确性,如有问题可以留言共同讨论。