Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getSingleResult returns proxy on native scalar query in hibernate

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?

like image 578
Ryan Avatar asked Jan 12 '12 21:01

Ryan


1 Answers

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.

like image 54
Tomasz Nurkiewicz Avatar answered Oct 19 '22 18:10

Tomasz Nurkiewicz