mysql - 在 HQL 中使用 CAST(columnName AS INT)

标签 mysql hibernate postgresql hql hibernate-4.x

我正在使用 Hibernate 4 和 Postgres

以下查询是两个表的连接,我需要将一个 varchar 列转换为整数,以便按顺序检索某些值。 HQL 支持这个吗?文档是这样说的,但我得到如下异常

Query query  = session.createQuery("SELECT DISTINCT a.attributeName, a.displayName, CAST(a.seqNo AS INTEGER) FROM Entitydefinitionconcrete a, Entitymasterconcrete b WHERE (a.entityTypeId=b.concreteEntityId AND b.baseType=:baseType) AND a.viewGrid=:flag ORDER BY CAST(a.seqNo AS INTEGER)");
query.setParameter("baseType","test");
query.setParameter("flag", true);
query.list();

异常:

HHH000203: processEqualityExpression() : No expression to process!
org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '.' near line 1, column 70 [SELECT DISTINCT a.attributeName, a.displayName, CAST(a.seqNo AS Types.INTEGER) FROM com.tcs.oss.tims.common.entityBeans.Entitydefinitionconcrete a, com.tcs.oss.tims.common.entityBeans.Entitymasterconcrete b WHERE (a.entityTypeId=b.concreteEntityId AND b.baseType=:baseType) AND a.viewGrid=:flag ORDER BY CAST(a.seqNo AS INTEGER)]
     at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
    at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
    at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:278)
   at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)

最佳答案

DOC说,

cast(... as ...), where the second argument is the name of a Hibernate type, and 
extract(... from ...) if ANSI cast() and extract() is supported by the underlying database

E.G : cast(propName as integer)

关于mysql - 在 HQL 中使用 CAST(columnName AS INT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27441094/

相关文章:

mysql - Tomcat 7 和 Idea ide 的 JDBC 问题

php - 将 mySQL_pconnect 转换为 mySQLi_connect 时出现问题

java - 在 hibernate 中添加一个懒惰的 child

java - 从 C3P0 连接池获取 DataSource/Connection

java - Postgresql JDBC 驱动程序中的批量更新在自动提交中回滚

mysql - 考虑性能和友好 URL 时要实现的数据库 key 设计

mysql - SQL 迁移后 VBA 脚本不再工作

java - 使用jsp从数据库检索数据(Hibernate + Spring + Maven)

postgresql - 在文本 Postgres 中找到 "'"

postgresql - 直接使用PostgreSQL的多版本并发控制