java – 本机SQL抛出无效的列名异常
作者:互联网
我正在为我的应用程序使用Hibernate 3.2.5.
我有一个Dept表和一个Employees表.
Dept.java
private int deptId;
private String deptName;
private Map empMap = new HashMap();
//Getters and Setters
Employees.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>
下面是Native SQL的代码:
SQLQuery query = session.createSQLQuery("Select d.DEPT_ID, e.EMP_NAME from Dept d,Emp e where d.DEPT_ID = e.DEPT_ID")
.addEntity(Dept.class);
List<Dept> departments = query.list();
for(Dept depart :departments)
System.out.println(depart.getDeptName());
我得到了例外:
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.getString(OracleResultSet.java:1514)
at org.hibernate.type.StringType.get(StringType.java:18)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2096)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)
at org.hibernate.loader.Loader.getRow(Loader.java:1206)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
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)
... 7 more
当我直接在db中运行查询时,我得到了正确的输出,但在休眠时,它给出了一个无效的COlumn名称.我已确认列名称只是正确的.
请告诉我如何解决这个问题.
解决方法:
你在映射中有这个:
<column name="DEPT_NAME"></column>
但是在您的SQL中,Select和from之间没有这样的列:
session.createSQLQuery("Select d.DEPT_ID, e.EMP_NAME from Dept d,Emp e where d.DEPT_ID = e.DEPT_ID")
Hibernate没有可能绑定属性.
试试吧:
session.createSQLQuery("Select d.DEPT_ID, d.DEPT_NAME, e.EMP_NAME from Dept d,Emp e where d.DEPT_ID = e.DEPT_ID")
标签:java,orm,hibernate,hibernate-mapping 来源: https://codeday.me/bug/20191008/1870432.html