I have a table in Oracle where column SC_CUR_CODE is CHAR(3)
When I do:
Query q2 = em.createNativeQuery("select sc_cur_code, sc_amount from sector_costs");
q2.setMaxResults(10);
List<Object[]> rs2 = q2.getResultList();
for (Object[] o : rs2) {
System.out.println(">>> cur=" + o[0]);
}
I see cur=E
and cur=U
instead of cur=EUR
and cur=USD
o[0]
is a java.lang.Character
How can I get the full value EUR
and USD
?
It looks like Hibernate reads value of type
CHAR(n)
asCharacter
. Try to cast it toVARCHAR(n)
:When using Hibernate via
Session
interface, you can explcitly set a type of result withaddScalar()
instead (also accessible viaunwrap()
in JPA 2.0):There are plenty of unresolved issues related to this problem in Hibernate JIRA, starting from HHH-2220.
Here is an explanation by Max Rydahl Andersen from HHH-2220's comments:
If you have your native query described in Hibernate mappings configuration file, then you need to define
<return-scalar ...>
for each value returned. Note: You have to enumerate all returned values, as when you define the return types explicitly, autodiscovery is switched off and only declared columns are returned.