gpt4 book ai didi

java - Hibernate - 无法从表中读取值

转载 作者:行者123 更新时间:2023-12-01 14:22:53 24 4
gpt4 key购买 nike

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

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

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

最佳答案

您正在创建 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)

关于java - Hibernate - 无法从表中读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17395573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com