JPA + Hibernate: changing behavior of createNativeQuery () + getResultList ()?

What I'm going to describe is a bit of legacy code. Therefore, there are many things that I cannot touch / change.

This pseudocode works well in Jboss 4.0.3,

big_messy_sql = "select t1.f1 as somealias, t2.f2 as somehthingelse  from obj1 t1, obj2 t2 where crazy_conditions....";

Query query = entityManager.createNativeQuery(big_messy_sql);


List<Object> results = query.getResultList();

for (Object oRow : results) {
    Object[] r = (Object[]) oRow;
    // Do more crazy stuff

}

      

So it works.

I am now trying to upgrade my Jboss server to 5.1.0GA which will use a more modern version of hibernate

15:39:02,089 INFO  [Version] Hibernate Annotations 3.4.0.GA
15:39:02,167 INFO  [Environment] Hibernate 3.3.2.GA
15:39:02,183 INFO  [Environment] hibernate.properties not found
15:39:02,198 INFO  [Environment] Bytecode provider name : javassist
15:39:02,198 INFO  [Environment] using JDK 1.4 java.sql.Timestamp handling
15:39:02,495 INFO  [Version] Hibernate Commons Annotations 3.1.0.GA
15:39:02,511 INFO  [Version] Hibernate EntityManager 3.4.0.GA

      

Then I got this exception:

12:06:09,031 INFO  [BigDecimalType] could not read column value from result set: id; S0022: Invalid column name 'id'.

      

I believe this is because this version of hibernate was trying to map the result set to class obj1 and obj2 respectively. These Java classes are fetched from the database via JPA properly elsewhere in this application (i.e. we have all @Entity, @Table and @Column, etc., applied to the obj1 and obj2 classes.) Since the id columns are flattened to something else in this messy query, the ORM failed.

Here is my question:

Is there somewhere I can turn off the automatic matching of the original request?

I want to avoid going all nine yards of the SQL mapping definition for this messy beast.

+2


source to share


2 answers


Instead of using SQL mapping, you can achieve this using createSQLQuery and addEntity . Check http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querysql.html



+2


source


Add GET_COLUMN_LABEL_FOR_NAME=true

to your data source connection url:



<connection-url>jdbc:sybase:Tds:[hostname]:[port]?GET_COLUMN_LABEL_FOR_NAME=true</connection-url>

      

0


source







All Articles