gpt4 book ai didi

java - hibernate 子查询抛出空指针异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:42:01 25 4
gpt4 key购买 nike

我正试图从 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")));

最佳答案

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

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

关于java - hibernate 子查询抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15630633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com