Hibernate 的 <= 出现问题

  1. 问题模拟
select new map( e.name as name , e.salary as salary) from Emplpyee e where e.salary <= :salary

设置值后查询不出结果,足足查了一个来小时
后来在一步一步执行sql的时候发现问题

  1. 问题原因
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字段都设置成了字符串类型

  1. 解决方案
    数据库的salary字段以及实体类的salary字段都设置为数字类型(NUMBER,BigDecimal)
    之后的sql,就少了那个'',查询成功
select e.name as name,e.salary as salary from Employee e where e.salary <= 2000
posted @ 2021-01-29 08:56  微花  阅读(85)  评论(0编辑  收藏  举报

Loading