Hibernate createSQLquery的列名错误无效

时间:2013-06-19 17:07:32

标签: java hibernate oracle11g

这是我正在努力工作的较大版本的较小版本。当我运行查询时,我收到以下错误。我已尽量删除尽可能多的查询,试图找到有罪的专栏,但没有取得任何成功。 我正在使用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&amp; 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();

2 个答案:

答案 0 :(得分: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"}]

答案 1 :(得分:0)

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

d.ID as {d.id}

d.ID

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