While i am executing the following Native sql hibernate application , i am getting java.sql.SQLException: Invalid column name . I am using Oracle 11g . Also i have Employee table with these following columns.
this is the mapping file .
<!-- employee.hbm.xml -->
<?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="Employee" table="Employee">
<id name="empNumber" column="EMPLOYEE_NUMBER "></id>
<property name="empName"`enter code here` column="EMPLOYEE_NAME"></property>
<property name="empSal" column="EMPLOYEE_SALARY"></property>
<property name="deptNumber" column="DEPT_NUMBER"></property>
</class>
</hibernate-mapping>
//NativeSQLClient.java
import java.util.Iterator;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class NativeSQLClient {
@SuppressWarnings("unchecked")
public static void main(String[] ars) {
Session session = new Configuration().configure().buildSessionFactory()
.openSession();
SQLQuery query = session.createSQLQuery("select * from Employee");
query.addEntity(Employee.class);
List li = query.list();
Iterator iterator = li.iterator();
System.out.println("============================");
while (iterator.hasNext()) {
Employee employee = (Employee) iterator.next();
System.out.println(employee.getEmpName() + " "
+ employee.getEmpNumber() + " " + employee.getDeptNumber()
+ " " + employee.getEmpSal());
}
}
}
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select * from Employee
Exception in thread "main" 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:2214)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
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 NativeSQLClient.main(NativeSQLClient.java:14)
Caused by: java.sql.SQLException: Invalid column name
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:110)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:171)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:227)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:439)
at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3352)
at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:1883)
at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:424)
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.loader.Loader.getKeyFromResultSet(Loader.java:1088)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:553)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2211)
... 7 more
Problems comes because there is space as a last character of column name:
<id name="empNumber" column="EMPLOYEE_NUMBER "></id>
Most likely column name is EMPLOYEE_NUMBER
without additional spaces.