getSingleResult在hibernate的本地标量查询上返回代理 [英] getSingleResult returns proxy on native scalar query in hibernate

查看:498
本文介绍了getSingleResult在hibernate的本地标量查询上返回代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的JPA持久性提供者从EclipseLink 2.3切换到Hibernate 3.6.5.Final。问题出在本地查询。注意:这不是EclipseLink的问题。我试图从没有声明实体的表中获取标量值, String 。这里是代码:

  Query q = em.createNativeQuery(select foo where foo_id =?); 
q.setParameter(1,fooId);
String description =(String)q.getSingleResult();

使用Hibernate,我得到了一个 ClassCastException ,因为返回的对象实际上是一个代理对象。我不知道它是什么类型,但我知道它不是一个数组( object.getClass()。isArray()是false),我知道它不是列表( object instanceof List false )。



我缺少什么?



lockquote>

q.getSingleResult()。getClass()。getInterfaces() return?







类型 java.sql.Clob org.hibernate.engine.jdbc.WrappedClob java.io.Serializable code>。 [...]我甚至没有意识到该列是一个clob,我很惊讶EclipseLink正在为我转换字符串




看起来EclipseLink很聪明,可以转换CLOB(它实际上是一个非常长的字符序列,就像 String )到字符串,如果需要的话。有了Hibernate,必须明确地完成。我想这符合JPA规范。


I'm working on switching my JPA persistence provider from EclipseLink 2.3 to Hibernate 3.6.5.Final. The problem is with a native query. Note: this wasn't a problem with EclipseLink. I'm trying to obtain a scalar value, a String from a table that I don't have an Entity declared for. Here is the code:

Query q = em.createNativeQuery("select description from foo where foo_id = ?");
q.setParameter(1, fooId);
String description = (String)q.getSingleResult();

With Hibernate I get a ClassCastException because the object returned is actually a proxy object. I don't know what type it is, but I know it isn't an array (object.getClass().isArray() is false) and I know it isn't a List (object instanceof List is false).

What am I missing?

解决方案

Summarizing comments below the question:

What interfaces does q.getSingleResult().getClass().getInterfaces() return?


It is of type java.sql.Clob, org.hibernate.engine.jdbc.WrappedClob, and java.io.Serializable. [...] I didn't even realize the column was a clob and I'm surprised EclipseLink was doing the conversion to String for me


Looks like EclipseLink is smart enough to convert CLOB (which is actually a very long sequence of characters, just like String) to String if required. With Hibernate this must be done explicitly. I guess this complies to the JPA specification.

这篇关于getSingleResult在hibernate的本地标量查询上返回代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆