Java: Hibernate Criteria API order by nulls last

1.2k Views Asked by At

I'm using Hibernate Criteria API to get my search results on Employee table. I'm creating left joins to the search fields that are related to the Employee.

Criteria criteria = session.createCriteria(Employee.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

criteria.createAlias("locations", "loc", Criteria.LEFT_JOIN);
criteria.createAlias("designations", "desig", Criteria.LEFT_JOIN);
criteria.createAlias("project", "ps", Criteria.LEFT_JOIN);

Junction disjunction = Restrictions.disjunction();
disjunction.add(Restrictions.like("loc.locationName", "%" + loc + "%"));
disjunction.add(Restrictions.like("desig.designation", "%" + fa + "%"));
disjunction.add(Restrictions.like("ps.project", "%" + fa + "%"));

criteria.add(disjunction);

From the above code I do get the list but some of the employees might have 2 fields null. I want to the the employee with most not null fields on top. Is this the right approach?

I saw this question about NULLS LAST in hibernate: Hibernate order by with nulls last This question talks about the HHH-465 bug that wasn't fixed when this question was asked. But now they have fixed it. I couldn't find any implementation code for this though. Any help is appreciated. Thanks.

0

There are 0 best solutions below