SettingSpec { public static Specification<Setting> nameStartsWith(final String prefix) { return new Specification<Setting>() { @Override public Predicate toPredicate(Root<Setting> root, CriteriaQuery<?> query, CriteriaBuilder cb) { return cb.like(root.get(Setting_.name), prefix + "%"); } }; }  static Specification<Setting> nameStartsWith(final String prefix);  }
@Test public void testNameStartsWith() throws Exception { final Setting s1 = settingRepository.save(new Setting().setName("NAME/abc").setValue("val")); final Setting s2 = settingRepository.save(new Setting().setName("NAME/abc2").setValue("val")); final Setting s3 = settingRepository.save(new Setting().setName("OTHER/abc").setValue("val")); List<Setting> found = settingRepository.findAll(SettingSpec.nameStartsWith("NAME/")); List<String> ids = Lists.transform(found, SETTING_NAME_FUNCTION); assertEquals(ids.toString(), 2, found.size()); assertTrue(ids.contains(s1.getName())); assertTrue(ids.contains(s2.getName())); found = settingRepository.findAll(SettingSpec.nameStartsWith("OTHER/")); ids = Lists.transform(found, SETTING_NAME_FUNCTION); assertEquals(1, found.size()); assertTrue(ids.contains(s3.getName())); }