gpt4 book ai didi

java - Hibernate createSQLquery 的无效列名错误

转载 作者:行者123 更新时间:2023-11-29 03:32:51 37 4
gpt4 key购买 nike

这是我正在尝试处理的较大查询的较小版本。当我运行查询时,出现以下错误。我已尝试尽可能多地删除查询以尝试找到有罪的列,但没有取得任何成功。我正在使用 Spring 3.2.1、Hibernate 3.6 和 Oracle 11g

Caused by: java.sql.SQLException: Invalid column name
at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3711)
at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2763)
at oracle.jdbc.driver.OracleResultSet.getBigDecimal(OracleResultSet.java:368)
at org.hibernate.type.descriptor.sql.DecimalTypeDescriptor$2.doExtract(DecimalTypeDescriptor.java:62)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)

查询:

String query = "SELECT d.ID as {d.id}, d.DETAIL_ORDER as {d.detailOrder}, g.ID as {g.id}, g.NAME as {g.name} " +
"FROM EVAL_MASTER_EVAL_DETAIL d " +
"JOIN EVAL_QUESTION_GROUP g ON d.GROUP_ID = g.ID " +
"WHERE d.ACTIVE = 'Y' " +
"AND d.MASTER_EVAL_ID = :evalId" +
" ORDER BY d.DETAIL_ORDER ASC";

details = session.createSQLQuery(query).addEntity("d", EvalMasterEvalDetail.class)
.addEntity("g", EvalQuestionGroup.class).setParameter("evalId", evalId).list();

生成的 Hibernate 查询:

Hibernate: SELECT d.ID as ID29_0_, d.DETAIL_ORDER as DETAIL5_29_0_, g.ID as ID34_1_, g.NAME as NAME34_1_ FROM EVAL_MASTER_EVAL_DETAIL d JOIN EVAL_QUESTION_GROUP g ON d.GROUP_ID = g.ID WHERE d.ACTIVE = 'Y' AND d.MASTER_EVAL_ID =  ? ORDER BY d.DETAIL_ORDER ASC

映射(验证名称)

<hibernate-mapping>
<class name="org.anes.surveys.domain.EvalMasterEvalDetail" table="EVAL_MASTER_EVAL_DETAIL">
<id name="id" type="big_decimal">
<column name="ID" precision="22" scale="0" />
<generator class="sequence-identity" >
<param name="sequence">EVAL_MASTER_EVAL_DETAIL_SEQ</param>
</generator>
</id>
<many-to-one name="evalQuestionGroup" class="org.anes.surveys.domain.EvalQuestionGroup" fetch="select">
<column name="GROUP_ID" precision="22" scale="0" />
</many-to-one>
<many-to-one name="evalMasterEvaluation" class="org.anes.surveys.domain.EvalMasterEvaluation" fetch="select">
<column name="MASTER_EVAL_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
<property name="detailOrder" type="big_decimal">
<column name="DETAIL_ORDER" precision="22" scale="0" not-null="true" />
</property>
<property name="active" type="string">
<column name="ACTIVE" length="1" not-null="true" />
</property>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="org.anes.surveys.domain.EvalQuestionGroup" table="EVAL_QUESTION_GROUP">
<id name="id" type="big_decimal">
<column name="ID" precision="22" scale="0" />
<generator class="sequence-identity" >
<param name="sequence">EVAL_QUESTION_GROUP_SEQ</param>
</generator>
</id>
<property name="name" type="string">
<column name="NAME" not-null="true" />
</property>
<set name="evalMasterEvalDetails" table="EVAL_MASTER_EVAL_DETAIL" inverse="true" lazy="true" fetch="select">
<key>
<column name="GROUP_ID" precision="22" scale="0" />
</key>
<one-to-many class="org.anes.surveys.domain.EvalMasterEvalDetail" />
</set>
</class>
</hibernate-mapping>

编辑:尝试了以下但仍然出现无效列错误。 ACTIVE & d.active 是字符串。

String query = "SELECT ACTIVE as {d.active} " +
"FROM EVAL_MASTER_EVAL_DETAIL";
details = session.createSQLQuery(query).addEntity("d", EvalMasterEvalDetail.class).list();

String query = "SELECT d.ID " +
"FROM EVAL_MASTER_EVAL_DETAIL d ";
details = session.createSQLQuery(query).addEntity("d", EvalMasterEvalDetail.class).list();

String query = "SELECT ID " +
"FROM EVAL_MASTER_EVAL_DETAIL";
details = session.createSQLQuery(query).addEntity("d", EvalMasterEvalDetail.class).list();

这有效,但我只得到标量值:

String query = "SELECT ID " +
"FROM EVAL_MASTER_EVAL_DETAIL";
details = session.createSQLQuery(query).list();

最佳答案

这是我最终做的事情。

-我使用 HQL 而不是我的原生 SQL(引用对象字段而不是数据库列)

-创建了一个带有 getter 和 setter 的新域类来定义/保存我的查询结果(类包含来自 5 个不同对象/表的字段)

public class EvalForm {
private BigDecimal detailId;
private BigDecimal detailOrder;
private BigDecimal groupId;
private String groupName;
}

-使用我的新类的字段名作为列别名

String query = "SELECT d.id as detailId, d.detailOrder as detailOrder, g.id as groupId, g.name as groupName....

- 添加 setResultTransformer 以指向我的新类

details = session.createQuery(query).setParameter("evalId", evalId).setResultTransformer(Transformers.aliasToBean(EvalForm.class)).list();

结果呢?非常适合 JSON 输出。

[{"detailId":44,"detailOrder":0,"groupId":128,"groupName":"My dope name"},{"detailId":42,"detailOrder":1,"groupId":68,"groupName":"qGroup AJAX"},{"detailId":81,"detailOrder":2,"groupId":68,"groupName":"qGroup AJAX"}]

关于java - Hibernate createSQLquery 的无效列名错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17197246/

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