2010-12-28 12 views
5

Estoy tratando de ejecutar la consulta nativa sql utilizando hibernate 3.3.2.GA.Hibernate error de SQL nativo

Tengo la siguiente consulta.

session.createSQLQuery("SELECT {dept1.*}, {dept2.*} FROM Dept d1, Dept d2 WHERE d1.deptId = d2.deptId"). 
    addEntity("dept1",com.test.pojo.Dept.class). 
    addEntity("dept2",com.test.pojo.Dept.class). 
    list(); 

archivo de asignación para la clase Dept es

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
<class name="com.test.pojo.Dept"> 
    <id column="deptId" name="deptId" type="long"> 
    <generator class="native"/> 
    </id> 
    <version name="version" access="field" column="version"></version> 
    <property name="deptName" type="string" column="deptName"/> 

    <set name="emps" cascade="all" inverse="true"> 
    <key column="deptId"></key> 
    <one-to-many class="com.test.pojo.Emp"/> 
    </set> 
</class> 
</hibernate-mapping> 

Pero por qué me sale error? Se está convirtiendo mi consulta a

SELECT dept1.**deptId as deptId1_0_, **dept1.**version as version1_0_, **dept1.**deptName as deptName1_0_, **dept2.**deptId as deptId1_1_, **dept2.**version as version1_1_, **dept2.**deptName as deptName1_1_ **FROM Dept d1, Dept d2 WHERE d1.deptId = d2.deptId.

Hibernate: SELECT dept1.deptId as deptId1_0_, dept1.version as version1_0_, dept1.deptName as deptName1_0_, dept2.deptId as deptId1_1_, dept2.version as version1_1_, dept2.deptName as deptName1_1_ FROM Dept d1, Dept d2 WHERE d1.deptId = d2.deptId 20:43:41,109 WARN JDBCExceptionReporter:100 - SQL Error: 904, SQLState: 42000 20:43:41,109 ERROR JDBCExceptionReporter:101 - ORA-00904: "DEPT2"."DEPTNAME": invalid identifier

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
    at org.hibernate.loader.Loader.doList(Loader.java:2235) 
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129) 
    at org.hibernate.loader.Loader.list(Loader.java:2124) 
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:312) 
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1723) 
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165) 
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:175) 
    at com.test.test.Test1.main(Test1.java:96) 
Caused by: java.sql.SQLException: ORA-00904: "DEPT2"."DEPTNAME": invalid identifier 
    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134) 
    at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289) 
    at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:590) 
    at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1973) 
    at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:850) 
    at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2599) 
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2963) 
    at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:658) 
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:584) 
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208) 
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1812) 
    at org.hibernate.loader.Loader.doQuery(Loader.java:697) 
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) 
    at org.hibernate.loader.Loader.doList(Loader.java:2232) 
    ... 7 more 

Respuesta

1

Prueba Google con los códigos ORA-XXX. De http://www.dba-oracle.com/t_ora_00904_string_invalid_identifier.htm

Question: I am running a SQL statement and I get a SQL*Plus error ORA-00904 invalid identifier.

Answer: When ORA-00904 occurs, you must enter a valid column name as it is either missing or the one entered is invalid. The "invalid identifier" most common happens when you are referencing an invalid alias in a select statement. The Oracle docs note this on the ORA-00904 error:

+0

No, el problema es la conversión de la consulta de Hibernate a no válida. Me refiero a la documentación de referencia de hibernación. – Neel

3

Debe ser

session.createSQLQuery(
    "SELECT {dept1.*}, {dept2.*} FROM Dept dept1, Dept dept2 WHERE dept1.deptId = dept2.deptId") 
    .addEntity("dept1",com.test.pojo.Dept.class) 
    .addEntity("dept2",com.test.pojo.Dept.class) 
    .list(); 

Si fue engañado por la documentación (18.1.4. Returning multiple entities), hay un error allí (HHH-2976), no dude en votar por ella.

+0

Creo que d1.deptId y d2.deptID también deberían modificarse a departamento * ;-) –

Cuestiones relacionadas