java JPA+Hibernate:createNativeQuery() + getResultList() 行为的变化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1558297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA+Hibernate: Change of createNativeQuery() + getResultList() behaviour?
提问by Anthony Kong
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,
这个伪代码在 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
现在我正在尝试将 Jboss 服务器升级到 5.1.0GA,这将使用更新版本的 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.
我相信这是因为这个版本的 hibernate 试图将结果集分别映射到类 obj1 和 obj2 中。这些 java 类是通过 JPA 在这个应用程序的其他地方正确地从数据库中检索的(即,我们确实将所有 @Entity、@Table 和 @Column 等应用于类 obj1 和 obj2。)因为 id 列别名为这个凌乱的查询,ORM 失败了。
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.
我想避免为这个凌乱的野兽定义 SQL 映射的整个九码。
采纳答案by n002213f
Instead of using SQL mapping, you can achieve this using createSQLQueryand addEntity. Check out http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querysql.html
您可以使用createSQLQuery和addEntity来实现这一点,而不是使用 SQL 映射。查看http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querysql.html
回答by Aakashdeep Singh
Add GET_COLUMN_LABEL_FOR_NAME=truein the connection URL of the datasource:
GET_COLUMN_LABEL_FOR_NAME=true在数据源的连接URL中添加:
<connection-url>jdbc:sybase:Tds:[hostname]:[port]?GET_COLUMN_LABEL_FOR_NAME=true</connection-url>

