Hibernate Criteria多个分离查询的分离查询空指针问题

时间:2015-01-07 06:00:22

标签: hibernate

我尝试实现多个分离查询以获取userId。但是它会抛出空指针异常。我尝试了很多解决方案,但是我没有得到正确的输出。

DetachedCriteria getShortListUser = DetachedCriteria
    .forClass(ShortListedProfileVO.class,"ug")
    .setProjection(Property.forName("ug.candidateId"))
    .add(Restrictions.eq("ug.employerId.parentUserId",employerIdParentId));

DetachedCriteria exampleSubquery = DetachedCriteria
    .forClass(PreferedLocationsVO.class,"pg")
    .setProjection(Property.forName("pg.userId"))   
    .add(Restrictions.in("pg.cityId.id",preferredLocations))
    .add(Property.forName("pg.userId").notIn(getShortListUser));

此处我附上了例外

显示java.lang.NullPointerException     在org.hibernate.criterion.SubqueryExpression.getTypedValues(SubqueryExpression.java:80)     在org.hibernate.loader.criteria.CriteriaQueryTranslator.getQueryParameters(CriteriaQueryTranslator.java:251)     在org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:55)     在org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:334)     在org.hibernate.loader.criteria.CriteriaJoinWalker。(CriteriaJoinWalker.java:71)     在org.hibernate.loader.criteria.CriteriaLoader。(CriteriaLoader.java:68)     在org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)     在org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)     在com.loginhire.employer.dao.EmployerSearchEmployeeDAO.SearchEmployee(EmployerSearchEmployeeDAO.java:79)     在com.loginhire.employer.action.VacancyAction.search(VacancyAction.java:612)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:601)     在com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)     在com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)     在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)     在org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)     在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)     at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)     在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)

2 个答案:

答案 0 :(得分:0)

如果没有完整的(或至少相关的)映射(带注释的实体),很难判断。但至少有两个可疑部分。

在限制中链接的属性是" ug.employerId.parentUserId"

...
.add(Restrictions.eq("ug.employerId.parentUserId",employerIdParentId));

老实说,我希望你的模型是" ug.employer.parentUser.Id"

...
.add(Restrictions.eq("ug.employer.parentUser.Id",employerIdParentId));

此外,要求employer表示我们需要加入 employer table 。这不是隐含的,所以像:

DetachedCriteria getShortListUser = DetachedCriteria
.forClass(ShortListedProfileVO.class,"ug")
.createCriteria("ug.employer", "employer") // JOIN THE employer
// or alias
.createAlias("ug.employer", "employer") // JOIN THE employer
...

选中此hibernate - createCriteria or createAlias?

还可以尝试使用这些部分来获取文档(与Hiberante 3相关)

答案 1 :(得分:0)

您将创建多个分离标准,并应附加会话条件。

DetachedCriteria getShortListUser = DetachedCriteria
    .forClass(ShortListedProfileVO.class,"ug")
    .setProjection(Property.forName("ug.candidateId"))
    .add(Restrictions.eq("ug.employerId.parentUserId",employerIdParentId));

Criteria criteria = getSession().createCriteria(PreferedLocationsVO.class)
    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
    .add(Restrictions.or(
        Subqueries.propertyIn("userId", getShortListUser)));

如果您有多个子查询,

Criteria criteria = getSession().createCriteria(PreferedLocationsVO.class)
    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
    .add(Restrictions.or(
        Subqueries.propertyIn("userId", getShortListUser),
        Subqueries.propertyIn("employeeId", anotherDetachedCriteria)));

More Example