1

我正在尝试SUBSTR使用 Criteria API 在 Hibernate 中进行查询:

        Type[] type = {Hibernate.INTEGER,Hibernate.INTEGER};
        Criteria criteria = session.createCriteria(Dept.class).setProjection(Projections.projectionList().add(Projections.sqlProjection("substr(DEPT_NAME,1,3) as name", new String[]{"name"}, type)));         
        List<String> list1 = criteria.list();

但我得到了例外:

org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at com.infy.jdbc.HibernateStartup.main(HibernateStartup.java:196)
Caused by: java.sql.SQLException: Fail to convert to internal representation
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:207)
at oracle.jdbc.driver.CharCommonAccessor.getInt(CharCommonAccessor.java:131)
at oracle.jdbc.driver.OracleResultSetImpl.getInt(OracleResultSetImpl.java:520)
at oracle.jdbc.driver.OracleResultSet.getInt(OracleResultSet.java:1562)
at org.hibernate.type.IntegerType.get(IntegerType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:189)
at org.hibernate.loader.criteria.CriteriaLoader.getResultColumnOrRow(CriteriaLoader.java:107)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:606)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
... 6 more

在控制台中打印的 SQL 查询是:

Hibernate: select substr(DEPT_NAME,1,3) as name from dept this_

当我在我的数据库中执行此操作时,它工作正常。

请让我知道这个例外的原因。

问候,

4

1 回答 1

0

异常Caused by: java.sql.SQLException: Fail to convert to internal representation 表示在处理字段值时数据类型不匹配(例如,将 char 分配给 int)。

于 2013-01-05T07:04:59.837 回答