0

I've tried to implement multiple detached queries to get the userId. But it throws null pointer exception.I've tried many solution, but I would not get the proper output.

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

Here with I've attached the Exception

java.lang.NullPointerException at org.hibernate.criterion.SubqueryExpression.getTypedValues(SubqueryExpression.java:80) at org.hibernate.loader.criteria.CriteriaQueryTranslator.getQueryParameters(CriteriaQueryTranslator.java:251) at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:55) at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:334) at org.hibernate.loader.criteria.CriteriaJoinWalker.(CriteriaJoinWalker.java:71) at org.hibernate.loader.criteria.CriteriaLoader.(CriteriaLoader.java:68) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550) at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283) at com.loginhire.employer.dao.EmployerSearchEmployeeDAO.SearchEmployee(EmployerSearchEmployeeDAO.java:79) at 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) at java.lang.reflect.Method.invoke(Method.java:601) at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453) at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255) at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) at 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) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)

2 Answers2

0

Without your complete (or at least related) mapping (annotated Entities) it is hard to judge. But there are at least two suspected parts.

The property chaining in restrictions is "ug.employerId.parentUserId"

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

Honestly, I would expect that your model is "ug.employer.parentUser.Id"

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

And also, asking for employer means, that we need to join the employer table. That is not done implicitly so something like:

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

Check this hibernate - createCriteria or createAlias?

Try also this parts for documentation (related to Hiberante 3 as well)

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Thank you so much for your reply. I've tried to use 'createAlias', but it shows unknown entity or column error.It doesn't support it. I'm using hibernate3 version. – Umar Mukthar Kadher Ibrahim Jan 07 '15 at 06:49
  • The createAlias is must in this case. For sure. The first part must express the relation "ug.employer", the second is the alias name, which we can use later (for projectins, restrcitions) instead of complete path from root. If it is still not working... try to check the doc, I updated answer with links – Radim Köhler Jan 07 '15 at 06:51
0

You will create multiple detached criteria and you should attach with session criteria.

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

If you have multiple sub queries,

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

More Example

ashokhein
  • 1,048
  • 12
  • 38