title: Java: Hibernate Criteria API order by nulls last
body: <p>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.</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>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?</p>
  
  <p>I saw this question about NULLS LAST in hibernate: <a href="https://stackoverflow.com/questions/3683174/hibernate-order-by-with-nulls-last">Hibernate order by with nulls last</a>
  This question talks about the <a href="https://hibernate.onjira.com/browse/HHH-465" rel="nofollow noreferrer">HHH-465</a> 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.</p>