我使用的是postgresql
StringBuffer sb = new StringBuffer();
sb.append("SELECT a.name as name,.....FROM 表名 a,表名 b WHERE 1 = 1 AND a.card_no = b.card_no
AND a.out_time <=[color=red] now()::TIMESTAMP[/color] AND a.out_time >= [color=red]CURRENT_DATE::TIMESTAMP[/color] AND b.card_type = 0")
this.getSession().createSQLQuery(query).lsit();
报org.hibernate.QueryException: Not all named parameters have been set: [:timestamp]
以上sql查询语句在postgresql查询无问题,但在hibernate就报错,估计是hibernate底层的原因。
使用[color=red][b]cast as[/b][/color]解决
SELECT
a.name as name,
.......
FROM
表名 a,
表名b
WHERE
1 = 1
AND a.card_no = b.card_no
AND a.out_time <= [b][color=red]cast(now() as TIMESTAMP)[/color][/b]
AND a.out_time >= [color=red]cast (CURRENT_DATE as TIMESTAMP)[/color]
AND b.card_type = 0
StringBuffer sb = new StringBuffer();
sb.append("SELECT a.name as name,.....FROM 表名 a,表名 b WHERE 1 = 1 AND a.card_no = b.card_no
AND a.out_time <=[color=red] now()::TIMESTAMP[/color] AND a.out_time >= [color=red]CURRENT_DATE::TIMESTAMP[/color] AND b.card_type = 0")
this.getSession().createSQLQuery(query).lsit();
报org.hibernate.QueryException: Not all named parameters have been set: [:timestamp]
以上sql查询语句在postgresql查询无问题,但在hibernate就报错,估计是hibernate底层的原因。
使用[color=red][b]cast as[/b][/color]解决
SELECT
a.name as name,
.......
FROM
表名 a,
表名b
WHERE
1 = 1
AND a.card_no = b.card_no
AND a.out_time <= [b][color=red]cast(now() as TIMESTAMP)[/color][/b]
AND a.out_time >= [color=red]cast (CURRENT_DATE as TIMESTAMP)[/color]
AND b.card_type = 0
本文探讨了在使用Hibernate进行ORM操作时遇到的一个特定问题:在PostgreSQL数据库中运行的SQL查询在Hibernate中报错。通过分析并应用正确的类型转换方式,解决了查询语句在不同环境下执行的兼容性问题。

被折叠的 条评论
为什么被折叠?



