11

I am using Hibernate 3.2.5 for my application.

I have a Dept table and an Employees table.

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 Mapping file

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

Below is the code for 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());

I am getting the exception:

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

When I run the query directly in the db, then I am getting the correct output but in hibernate, it is giving an Invalid COlumn name. I have confirmed that the column names are correct only.

Kindly let me know how to fix this issue.

user182944
  • 7,897
  • 33
  • 108
  • 174

3 Answers3

13

you have this in your mapping:

<column name="DEPT_NAME"></column>

but there is no such column in your sql between Select and from:

session.createSQLQuery("Select d.DEPT_ID, e.EMP_NAME from Dept d,Emp e where d.DEPT_ID = e.DEPT_ID")

Hibernate has no possibilitys to bind the attribute. Try it with this:

session.createSQLQuery("Select d.DEPT_ID, d.DEPT_NAME, e.EMP_NAME from Dept d,Emp e where d.DEPT_ID = e.DEPT_ID")
nano_nano
  • 12,351
  • 8
  • 55
  • 83
  • 1
    Why this kind of a behavior? What if it is a big table with lots of columns where I need some of the columns for my purpose? In that case also I have to add all the column names? Kindly explain. – user182944 Jun 26 '13 at 14:26
  • 1
    In this case you could use "select * from" – nano_nano Jun 26 '13 at 14:27
  • 2
    What if I need some of them and not all? Is it a disadv of Native SQL over HQL or criteria? – user182944 Jun 26 '13 at 14:29
  • 6
    I have this problem with a huge table where I only need some data. I cannot use * or d.* because that would request too much data and slows it down too much. What should I do? HQL and Criteria are no option here. – T3rm1 Feb 12 '14 at 10:43
1

I had the same issue.

The reason is my column name in annotation is not correct.

@Column(name = "CUSTOMER_NAME", length = 200)

should be changed to

@Column(name = "CUST_NM", length = 200)
Brian
  • 14,610
  • 7
  • 35
  • 43
Gordon Ma
  • 239
  • 2
  • 6
0

Here are my 2cents in addition to what Stefan Beike.

In your case, the table is small. But if you are writing a similar query for a large table, and if you don't want the entire columns to be fetched into the memory, writing a new DataTransferObject to use as your resultset-ref would be ideal.