JPA+Hibernate: Change of createNativeQuery() + getResultList() behaviour?
What I am going to describe is a bit of legacy code. So there开发者_如何转开发 is a lot of stuff I cannot touch/change.
This pseudo-code runs well the 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.
Now I am trying to upgrade the Jboss server to 5.1.0GA, which will use more up-to-date 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 it is because this version of hibernate tried to map the result set into class obj1 and obj2 respectively. These java classes are retrieved from database via JPA properly elsewhere in this application (ie. we do have all the @Entity, @Table and @Column etc applied to the class obj1 and obj2.) Since the id columns is aliased to something else in this messy query, the ORM failed.
Here comes my question:
Is there anywhere I can disable the auto mapping of the Native Query?
I wanna avoid going the whole nine yards of defining a SQL mapping for this messy beast.
Instead of using SQL mapping, you can achieve this using createSQLQuery and addEntity. Check out http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querysql.html
Add GET_COLUMN_LABEL_FOR_NAME=true
in the connection URL of the datasource:
<connection-url>jdbc:sybase:Tds:[hostname]:[port]?GET_COLUMN_LABEL_FOR_NAME=true</connection-url>
精彩评论