1

我正在为我的应用程序使用 Hibernate 3.2.5。

我有一个 Dept 表和一个Employees 表。

java部门

private int deptId;
private String deptName;
private Map empMap = new HashMap();
//Getters and Setters

雇员.java

private int empId;
private String empName;
private int deptId;
private int age;
private String sex;
private Dept dept;

HBM 映射文件

<class name="com.jdbc.Dept" table="dept">
  <id name="deptId" type="integer" column="DEPT_ID">
      <generator class="assigned"></generator>
  </id>  
  <property name="deptName">
      <column name="DEPT_NAME"></column>
  </property>    
  <map name="empMap" inverse="false" cascade="all" lazy="true">
      <key column="DEPT_ID"></key>
      <map-key formula="EMP_ID" type="integer"></map-key>
      <one-to-many class="com.jdbc.Employees"/>
  </map>

下面是原生 SQL 的代码:

SQLQuery query = session.createSQLQuery("Select d.*, e.* from Dept d,Emp e where d.DEPT_ID = e.DEPT_ID")
                .addEntity("department",Dept.class)
                .addJoin("e","department.empMap");
        List<Dept> departments = query.list();
        for(Dept depart :departments)
            System.out.println(depart.getDeptId());

例外:

org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
at com.jdbc.HibernateStartup.main(HibernateStartup.java:75)
Caused by: java.sql.SQLException: Invalid column name
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:207)
at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3295)
at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:1913)
at oracle.jdbc.driver.OracleResultSet.getInt(OracleResultSet.java:1562)
at org.hibernate.type.IntegerType.get(IntegerType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
at org.hibernate.persister.collection.AbstractCollectionPersister.readIndex(AbstractCollectionPersister.java:708)
at org.hibernate.collection.PersistentMap.readFrom(PersistentMap.java:258)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:1008)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:646)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:591)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)

在文档中,他们使用的是addJoin采用 1 个参数的方法,但对我来说,它要求提供两个参数(因为我使用的是 v3.2.5 而文档在 v3.3 上)

请让我知道如何解决这个问题。

4

1 回答 1

2

in 的第一个参数.addEntity("department",Dept.class)是一个表别名,查看您的 SQL 查询,正确的值 is"d"和 not "department"

您也应该在此处进行此更改:.addJoin("e","department.empMap");,替换"department.empMap""d.empMap".

于 2013-10-14T18:33:40.890 回答