RunDao extends BaseHibernateEntityDao<RunHibernateEntity> { public RunHibernateEntity findLastInSequenceFor(EventHibernateEntity event) { Class<RunHibernateEntity> runClass = RunHibernateEntity.class; CriteriaBuilder builder = currentSession().getCriteriaBuilder(); CriteriaQuery<RunHibernateEntity> criteriaQuery = builder.createQuery(runClass); Root<RunHibernateEntity> runRoot = criteriaQuery.from(runClass); criteriaQuery.select(runRoot); criteriaQuery.where(builder.equal(runRoot.get("event"), event)); criteriaQuery.orderBy(builder.desc(runRoot.get("sequence"))); Query<RunHibernateEntity> query = currentSession().createQuery(criteriaQuery); query.setMaxResults(1); try { return query.getSingleResult(); } catch (NoResultException nre) { return null; } } @Inject  RunDao(SessionFactory sessionFactory); @Override void create(RunHibernateEntity entity); @Override List<RunHibernateEntity> findAll(); @Override RunHibernateEntity findById(String id); RunHibernateEntity findLastInSequenceFor(EventHibernateEntity event); RunHibernateEntity findFirstInSequenceWithoutRawTime(EventHibernateEntity event); List<RunHibernateEntity> getAllWith(EventHibernateEntity event); List<RunHibernateEntity> getAllWith(RegistrationHibernateEntity registration);  }
@Test public void itShouldFindLastInSequenceNullWhenNoneExists() { daoTestRule.inTransaction(() -> { RunHibernateEntity lastInSequenceForEvent = dao.findLastInSequenceFor(prerequisites.event); assertThat(lastInSequenceForEvent).isNull(); }); }