PointOfEntryService extends AbstractInfrastructureAdoService<PointOfEntry> { public List<PointOfEntry> getByName(String name, District district) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<PointOfEntry> cq = cb.createQuery(getElementClass()); Root<PointOfEntry> from = cq.from(getElementClass()); Predicate filter = cb.or( cb.equal(cb.trim(from.get(PointOfEntry.NAME)), name.trim()), cb.equal(cb.lower(cb.trim(from.get(PointOfEntry.NAME))), name.trim().toLowerCase())); if (!PointOfEntryDto.isNameOtherPointOfEntry(name.trim())) { filter = cb.and(filter, cb.equal(from.get(PointOfEntry.DISTRICT), district)); } cq.where(filter); return em.createQuery(cq).getResultList(); } PointOfEntryService(); List<PointOfEntry> getAllByDistrict(District district, boolean includeOthers); List<PointOfEntry> getByName(String name, District district); Predicate buildCriteriaFilter(PointOfEntryCriteria criteria, CriteriaBuilder cb, Root<PointOfEntry> pointOfEntry); @SuppressWarnings("rawtypes") @Override Predicate createUserFilter(CriteriaBuilder cb, CriteriaQuery cq, From<?, PointOfEntry> from);  }
@Test public void testGetByName() { Region region = creator.createRegion("Region"); District district = creator.createDistrict("District", region); District otherDistrict = creator.createDistrict("Other District", region); creator.createPointOfEntry("Point of Entry", region, district); assertThat(getPointOfEntryService().getByName("Point of Entry", district), hasSize(1)); assertThat(getPointOfEntryService().getByName(" Point of Entry ", district), hasSize(1)); assertThat(getPointOfEntryService().getByName("point of entry", district), hasSize(1)); assertThat(getPointOfEntryService().getByName("POINT OF ENTRY", district), hasSize(1)); assertThat(getPointOfEntryService().getByName("Point of Entry", otherDistrict), empty()); assertThat(getPointOfEntryService().getByName("Redcliffe Harbour", district), empty()); }