I agree that setting maxresult would cause that situation. But I'm facing the problem right now and I don't have this set. I migrated from plain hibernate to JPA (entity manager) based. Before I switched it worked fine.
Mapping:
@NamedNativeQuery(name = "X", query = "{? = call XYZ(?, ?)}", resultSetMapping = "XM", hints = { @QueryHint(name = "org.hibernate.callable", value = "true") })
@SqlResultSetMapping(name = XM", columns = { @ColumnResult(name = "x") })
DAO:
final Query q = this.em.createNamedQuery("X");
q.setParameter(1, "whatever");
q.setParameter(2, "whatever");
final String x = (String) q.getSingleResult();
And that's exactly it. "this.em" is a injected entity manager that works flawless. The log shows:
[main] org.hibernate.impl.SessionImpl: SQL query: {? = call XYZ(?, ?)}
and then
[main] org.hibernate.SQL: select * from ( {? = call XYZ(?, ?)} ) where rownum <= ?
And it just does not make any sense to me. I tested if switching between org.hibernate.annotations.NamedNativeQuery and javax.persistence.NamedNativeQuery would make any change but it doesn't.
Can anyone tell me what I'm doing wrong?
Edit:
I found a quick workaround: using
Code:
final String x = (String) q.getResultList().get(0);
instead of
Code:
final String x = (String) q.getSingleResult();
works. I'm not sure whether or not this is a bug in hibernate. At least it's irritating that QueryImpl is calling q.list() in getSingleResult() and fiddling with setMaxResults(). I assumed it would just forward the call to uniqueResult().