hibernate子查询抛出Null指针异常

时间:2013-03-26 06:22:27

标签: java sql spring hibernate sql-server-2005

我试图从hibernate中触发子查询,但它显示NPE :-(任何人都知道如何解决它?下面是代码:

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);

堆栈追踪:

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)

通过向内部查询添加投影来解决该问题。

commCriteria.createAlias("A1.b", "b1");
commCriteria.setProjection(Projections.projectionList().add(Projections.property("b1.c")));

2 个答案:

答案 0 :(得分:10)

通过向内部查询添加投影来解决该问题。

commCriteria.createAlias("A1.b", "b1");
commCriteria.setProjection(Projections.projectionList().add(Projections.property("b1.c")));

答案 1 :(得分:0)

这是Hibernate https://hibernate.atlassian.net/browse/HHH-993

中的一个错误

正如@Renjith所说,它是通过向DetachedCriteria添加投影来解决的。