egaga
egaga

Reputation: 21722

Hibernate exception for native query

I have a Hiberante native query that fetches a large amount of data. Until now, it has worked without hiccups but for some reason for particular data I get the exception given below. How should I try to find the reason for this?

My native query is of form:

    Query query = entityManager.createNativeQuery(sql);
    query.setParameter(1, foo);
    List<Object[]> results = query.getResultList();

The actual query consists of two inner join, multiple left outer joins, a where condition and order by of three columns. It does not have any calls to sql functions. It does use indexes since it's a big query. This problem occurred with Hibernate 4 beta 5 and also with 4.1.1, using MySQL 5.5

There are also other queries but the exception is directly related to the moment when query.getResultList is called for that native query. Could it be that Hibernate forces some other query execution then also (because of laziness or something else?)

Caused by: java.lang.NullPointerException
    at org.hibernate.engine.jdbc.internal.proxy.ResultSetProxyHandler.getJdbcServices(ResultSetProxyHandler.java:57) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.engine.jdbc.internal.proxy.AbstractResultSetProxyHandler.continueInvocation(AbstractResultSetProxyHandler.java:110) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:80) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at $Proxy305.wasNull(Unknown Source)    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:66) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:269) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:265) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.extract(CustomLoader.java:549) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.custom.CustomLoader$ResultRowProcessor.buildResultRow(CustomLoader.java:499) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.custom.CustomLoader$ResultRowProcessor.buildResultRow(CustomLoader.java:478) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:375) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:668) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.Loader.doQuery(Loader.java:860) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.Loader.doList(Loader.java:2449) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.Loader.doList(Loader.java:2435) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.Loader.list(Loader.java:2271) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:331) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1571) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:224) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:156) [hibernate-core-4.0.0.Beta5.jar:4.0.0.Beta5]
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:252) [hibernate-entitymanager-4.0.0.Beta5.jar:4.0.0.Beta5]
    at com.foo.bar.getResults(MyResultHandler.java:114) [classes:]
    at sun.reflect.GeneratedMethodAccessor16978.invoke(Unknown Source) [:1.7.0]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [:1.7.0]
    at java.lang.reflect.Method.invoke(Method.java:601) [:1.7.0]
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:51)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:370) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:114) [jboss-as-weld-7.0.1.Final.jar:7.0.1.Final]
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:122) [jboss-as-weld-7.0.1.Final.jar:7.0.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:45) [jboss-as-jpa-7.0.1.Final.jar:7.0.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.ejb3.component.stateful.StatefulSessionSynchronizationInterceptor.processInvocation(StatefulSessionSynchronizationInterceptor.java:132)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:44)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.ee.component.ViewDescription$ComponentDispatcherInterceptor.processInvocation(ViewDescription.java:202)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.jpa.interceptor.SFSBInvocationInterceptor.processInvocation(SFSBInvocationInterceptor.java:58) [jboss-as-jpa-7.0.1.Final.jar:7.0.1.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.ejb3.component.stateful.StatefulComponentInstanceInterceptor.processInvocation(StatefulComponentInstanceInterceptor.java:61)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]
    at org.jboss.as.ejb3.component.session.SessionInvocationContextInterceptor$CustomSessionInvocationContext.proceed(SessionInvocationContextInterceptor.java:126)
    at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:233)
    ... 93 more

Upvotes: 0

Views: 1773

Answers (1)

ssedano
ssedano

Reputation: 8432

You might be expecting a primitive from a nullable column.

Hibernate uses reflection to create objects.

public class A {
    private long id;

    private int n;

    public void setN(int n) { this.n = n; }
    public int getN() { return this.n; }

    // id
}

If you retrieve an A object with DAO.get(id, A.class) it will mount like:

select a_.id, a_.n from A a_ where a_id = ? /* Where ? is the id */

Then it will do like (Note that this is just to illustrate my example):

A a = new A();

a.setId([a_.id])
a.setN([a_.n])

If you retrieve two rows:

id     n
 1     1
 2     NULL

it would try to instance an A object and set its n attribute with setN(null). Since the signature is a primitive this would raise an error.

Look at the table description to see if there is any nullable columns. If so, look for not expecting any primitives in setters.

That means that when hibernate executes the setPrimitive(null) // signature setPrimitive(long l) fails.

Upvotes: 1

Related Questions