In hibernate UniqueResult() and list() throws NullPointerException

269 views Asked by At

I am expecting a single value or null in call of a NamedQuery where query.list() and query.uniqueResult() returns NullPointerException. I am getting session from SessionFactory and there is no blank spaces in the parameters passed but not sure why null pointer exception is coming.

Some.withSession { session ->
                some= session.getNamedQuery('someNamedQueryGetCall')
                        .setString('xyz', xyz)
                        .setInteger('mid', mid)
                        .setString('abc', abc)
                        .uniqueResult()
            } 

Exception Details:

Caused by: java.lang.NullPointerException: null
    at java.util.ArrayList.<init>(ArrayList.java:178)
    at sun.reflect.GeneratedConstructorAccessor220.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrConstructorNewInstance(ReflectiveInterceptor.java:1076)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:110)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:250)

I know this is a groovy code but hibernate should work as usual. Not sure how can I handle if empty collection is coming and breaking the code which I am suspecting. I tried for list() also same NullPointerException comes.

NamedQuery:

@NamedQuery(name = 'Some.someNamedQueryGetCall',
            query = """ FROM Some some
            WHERE some.xyz = :xyz
            and some.mid = :mid
            and some.abc = :abc
            """)

Mappings:

public class Some implements Serializable {

@Id
@SequenceGenerator(name = 'TABLENAME_SEQ_GENERATOR', allocationSize = 1, sequenceName = 'TABLENAME_SURROGATE_ID_SEQUENCE')
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = 'TABLENAME_SEQ_GENERATOR')
@Column(name = 'TABLENAME_SURROGATE_ID')
Long id

@Column(name = 'TABLENAME_XYZ')
String xyz

@Temporal(TemporalType.TIMESTAMP)
@Column(name = 'TABLENAME_ACTIVITY_DATE')
Date lastModified

/**
 * Foreign Key : FK_TABLENAME_OTHER_TABLE
 */
@ManyToOne
@JoinColumns([
        @JoinColumn(name = "TABLENAME_OTHER_TABLE_ID", referencedColumnName = "OTHER_TABLE_ID")
])
OtherTableClass otherTableClass


@Column(name = 'TABLENAME_MID')
Integer mid

@Column(name = 'TABLENAME_ABC')
String abc
0

There are 0 answers