Hibernate - 无法从表中读取值

时间:2013-06-30 22:54:55

标签: java sql hibernate java-ee

无法通过hibernate从表中读取数据。但在TOAD中使用相同的SQL

SQL:

SELECT distinct pub.asdfg, pub.list, pub.prod, pub.prod_desc  FROM  Pubsh pub  WHERE pub.prod in ('123456')

表格def:

CREATE TABLE "PUBSH"
(
   KEY decimal(22) NOT NULL,
   PROD varchar2(54) NOT NULL,
   PRODKEY decimal(22),
   PROD_DESC varchar2(360),
   PARTEY decimal(22) NOT NULL,
   LIST_TYPE varchar2(90),
   PUBLISH_DATE date,
   PUBSH_PERIOD varchar2(30) NOT NULL,
   PL_KEY decimal(22),
   LIST varchar2(750),
   EL_KEY decimal(22),
   NEL varchar2(60) }

控制台出错:

    org.hibernate.SQL - SELECT distinct pub.nel, pub.list, pub.prod, pub.prod_desc  FROM  PUBSH WHERE pub.prod in ('12345')
        org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
             org.hibernate.type.StringType - could not read column value from result set; Invalid column name
        org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1)
        org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
  org.hibernate.util.JDBCExceptionReporter - could not execute query [SELECT distinct pub.nel, pub.list, pub.prodr, pub.desc  FROM  PUBSH WHERE pub.prod in ('12345')]
    java.sql.SQLException: Invalid column name
            at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3711)
            at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2806)
            at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:498)
            at weblogic.jdbc.wrapper.ResultSet_oracle_jdbc_driver_OracleResultSetImpl.getString(Unknown Source)
            at org.hibernate.type.StringType.get(StringType.java:18)
            at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
            at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:189)
            at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.extract(CustomLoader.java:474)
            at org.hibernate.loader.custom.CustomLoader$ResultRowProcessor.buildResultRow(CustomLoader.java:420)
            at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:317)
            at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:606)

执行Hibernate的DAO方法:

public List<SearchResult> find(final String Query1) {  
       return (List<SearchResult>)getHibernateTemplate().execute(new HibernateCallback(){
         public Object doInHibernate(Session session) throws HibernateException{
           Query query1 =  session.createSQLQuery(channelQuery1)
                                   .addScalar("nel",Hibernate.STRING)
                                   .addScalar("price",Hibernate.STRING)
                                   .addScalar("prodr",Hibernate.STRING)
                                   .addScalar("prodcr",Hibernate.STRING)
                                   .setResultTransformer(Transformers.aliasToBean(SearchResult.class));

               return query1.list();
             }

请注意为什么我在访问通过休眠时出现无效的列名错误?

1 个答案:

答案 0 :(得分:1)

您正在创建SQL查询,但标量属性与表中声明的属性不同:

.addScalar("prodNbr",Hibernate.STRING)
.addScalar("prodDescr",Hibernate.STRING)

这些属性在表中声明为PROD_NBRPROD_DESC,因此请将代码更改为

.addScalar("CHANNEL",Hibernate.STRING)
.addScalar("PRICELIST",Hibernate.STRING)
.addScalar("PROD_NBR",Hibernate.STRING)
.addScalar("PROD_DESC",Hibernate.STRING)