oracle 休眠本机 SQL 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4548735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 02:59:10  来源:igfitidea点击:

Hibernate native SQL error

oraclehibernateora-00904

提问by Neel

I am trying to execute sql native query using hibernate 3.3.2.GA.

我正在尝试使用 hibernate 3.3.2.GA 执行 sql 本机查询。

I have following query.

我有以下查询。

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

Mapping file for Dept class is

Dept 类的映射文件是

<?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>

But why I get following error? It is converting my query to

但是为什么我收到以下错误?它正在将我的查询转换为

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 d2WHERE 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

选择dept1.**deptId 作为deptId1_0_,**dept1.**version 作为version1_0_,**dept1.**deptName 作为deptName1_0_,**dept2.**deptId 作为deptId1_1_,**dept2.**version 作为version1_1_,* *dept2.**deptName as deptName1_1_ **FROM Dept d1, Dept d2WHERE 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_1_HER DeptdEdd2 .deptId 20:43:41,109 WARN JDBCExceptionReporter:100 - SQL 错误:904,SQLState:42000 20:43:41,109 错误 JDBCExceptionReporter:101 - ORA-00904:“DEPT2”。“DEPTNAME”:无效标识符

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

回答by axtavt

It should be

它应该是

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

If you was misled by the documentation (18.1.4. Returning multiple entities), there is a bug there (HHH-2976), feel free to vote for it.

如果您被文档(18.1.4. Returning multiple entity)误导,那里有一个错误(HHH-2976),请随意投票。

回答by Aravind Yarram

Try Google with the ORA-XXX codes. From http://www.dba-oracle.com/t_ora_00904_string_invalid_identifier.htm

使用 ORA-XXX 代码试试 Google。来自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:

问题:我正在运行 SQL 语句,但收到 SQL*Plus 错误 ORA-00904 无效标识符。

答:当出现 ORA-00904 时,您必须输入一个有效的列名,因为它要么丢失,要么输入的无效。当您在 select 语句中引用无效别名时,“无效标识符”最常见。Oracle 文档在 ORA-00904 错误中指出了这一点: