0

无法通过休眠从表中读取数据。但同样的 SQL 在 TOAD 中工作

SQL:

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

表定义:

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();
             }

请告知为什么我在通过休眠访问时收到无效的列名错误?

4

1 回答 1

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)
于 2013-06-30T23:47:58.880 回答