Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate subquery throwing Null Pointer Exception

I am trying to fire a subquery from hibernate, but it is showing NPE:-(Anyone know how to resolve it? Below is the code:

DetachedCriteria criteria = DetachedCriteria.forClass(My.class,"A");
        criteria.createAlias("A.a", "a");
        criteria.createAlias("A.b", "b");
        if(cValue !=null && cValue.size()>0){
            criteria.add(Restrictions.in("b.c", cValue));
        }
        if(StringUtils.isNotEmpty(commodityList)){
            DetachedCriteria commCriteria  = DetachedCriteria.forClass(My.class,"A1");
            commCriteria.createAlias("A1.a", "a1");
            commCriteria.add(Restrictions.in("a1.name", commodityList.split(",")));
            criteria.add(Subqueries.propertyIn("b.c", commCriteria));
        }
        criteria.addOrder(Order.asc("b.c"));

        return getHibernateTemplate().findByCriteria(criteria);

Stack Trace:

java.lang.NullPointerException
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:318)
    at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:56)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:334)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
    at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
    at org.springframework.orm.hibernate3.HibernateTemplate$36.doInHibernate(HibernateTemplate.java:1065)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:1055)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:1048)
    at com.boeing.kmapi.base.skillset.dao.impl.EssSkillsetCommMapDAOImpl.getEssSkillsetCommMapforSkillsetIdList(EssSkillsetCommMapDAOImpl.java:51)
    at com.boeing.kmapi.base.skillset.service.impl.EssGenericReportServiceImpl.filterCommodityDetails(EssGenericReportServiceImpl.java:87)
    at com.boeing.kmapi.base.skillset.service.impl.EssGenericReportServiceImpl.getGenericReport(EssGenericReportServiceImpl.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)

The issue was resoled by adding projection to inner query.

commCriteria.createAlias("A1.b", "b1");
commCriteria.setProjection(Projections.projectionList().add(Projections.property("b1.c")));
like image 840
Renjith Avatar asked Mar 26 '13 06:03

Renjith


1 Answers

It is a bug in Hibernate https://hibernate.atlassian.net/browse/HHH-993

As @Renjith said, it is solved by adding a projection to the DetachedCriteria.

like image 140
lboullo0 Avatar answered Nov 03 '22 16:11

lboullo0