DistrictService extends AbstractInfrastructureAdoService<District> { public List<District> getByName(String name, Region region, boolean includeArchivedEntities) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<District> cq = cb.createQuery(getElementClass()); Root<District> from = cq.from(getElementClass()); Predicate filter = cb.or( cb.equal(cb.trim(from.get(District.NAME)), name.trim()), cb.equal(cb.lower(cb.trim(from.get(District.NAME))), name.trim().toLowerCase())); if (region != null) { filter = cb.and(filter, cb.equal(from.get(District.REGION), region)); } if (!includeArchivedEntities) { filter = cb.and(filter, createBasicFilter(cb, from)); } cq.where(filter); return em.createQuery(cq).getResultList(); } DistrictService(); List<District> getAllActiveByRegion(Region region); int getCountByRegion(Region region); List<District> getByName(String name, Region region, boolean includeArchivedEntities); @SuppressWarnings("rawtypes") @Override Predicate createUserFilter(CriteriaBuilder cb, CriteriaQuery cq, From<?, District> from); Predicate buildCriteriaFilter(DistrictCriteria criteria, CriteriaBuilder cb, Root<District> from);  }
@Test public void testGetByName() { Region region = creator.createRegion("Region"); Region otherRegion = creator.createRegion("Other Region"); creator.createDistrict("District", region); assertThat(getDistrictService().getByName("District", region, true), hasSize(1)); assertThat(getDistrictService().getByName(" District ", region, true), hasSize(1)); assertThat(getDistrictService().getByName("district", region, true), hasSize(1)); assertThat(getDistrictService().getByName("DISTRICT", region, true), hasSize(1)); assertThat(getDistrictService().getByName("District", otherRegion, true), empty()); assertThat(getDistrictService().getByName("Redcliffe", region, true), empty()); }