1

这是我正在尝试使用的较大查询的较小版本。当我运行查询时,我收到以下错误。我已经尝试尽可能多地删除查询以尝试找到有罪的列,但没有任何成功。我正在使用 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: 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();
4

2 回答 2

4

这就是我最终做的事情。

-我使用 HQL 而不是我的本机 SQL(引用对象字段而不是 db 列)

- 使用 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"}]
于 2013-06-20T18:32:06.147 回答
0

删除所有列别名。即改变:

d.ID as {d.id}

只是

d.ID

等等

我从未见过这样的 HQL 语法。它可能是有效的,但它肯定是不必要的。

于 2013-06-19T20:13:57.297 回答