MetadataSpecs { public static Specification<? extends AbstractMetadata> hasCategory(final MetadataCategory category) { return new Specification<Metadata>() { @Override public Predicate toPredicate(Root<Metadata> root, CriteriaQuery<?> query, CriteriaBuilder cb) { final Expression<Set<MetadataCategory>> categoriesPath = root.get(Metadata_.metadataCategories); return cb.isMember(category, categoriesPath); } }; } private  MetadataSpecs(); static Specification<? extends AbstractMetadata> hasSchemaId(final String schemaId); static Specification<? extends AbstractMetadata> hasOwner(final int owner); static Specification<? extends AbstractMetadata> hasMetadataId(final int metadataId); static Specification<? extends AbstractMetadata> hasMetadataUuid(final String uuid); static Specification<? extends AbstractMetadata> hasHarvesterUuid(final String harvesterUuid); static Specification<? extends AbstractMetadata> isOwnedByUser(final int userId); static Specification<? extends AbstractMetadata> hasSource(final String sourceUuid); static Specification<? extends AbstractMetadata> isType(final MetadataType type); static Specification<? extends AbstractMetadata> isHarvested(final boolean isHarvested); static Specification<? extends AbstractMetadata> hasMetadataIdIn(final Collection<Integer> mdIds); static Specification<? extends AbstractMetadata> hasMetadataUuidIn(final Collection<String> uuids); static Specification<? extends AbstractMetadata> hasType(final MetadataType metadataType); static Specification<? extends AbstractMetadata> isOwnedByOneOfFollowingGroups(final List<Integer> groups); static Specification<? extends AbstractMetadata> hasCategory(final MetadataCategory category); static Specification<? extends AbstractMetadata> hasExtra(final String extra); static Specification<? extends AbstractMetadata> isIso19139Schema();  }
@Test public void testHasCategory() throws Exception { final MetadataCategory cat1 = _categoryRepo.save(MetadataCategoryRepositoryTest.newMetadataCategory(_inc)); final MetadataCategory cat2 = _categoryRepo.save(MetadataCategoryRepositoryTest.newMetadataCategory(_inc)); final MetadataCategory cat3 = _categoryRepo.save(MetadataCategoryRepositoryTest.newMetadataCategory(_inc)); final MetadataCategory cat4 = _categoryRepo.save(MetadataCategoryRepositoryTest.newMetadataCategory(_inc)); final Metadata metadata = newMetadata(_inc); metadata.getCategories().add(cat1); metadata.getCategories().add(cat2); Metadata md1 = _repository.save(metadata); final Metadata metadata2 = newMetadata(_inc); metadata2.getCategories().add(cat1); metadata2.getCategories().add(cat3); Metadata md2 = _repository.save(metadata2); final Metadata metadata3 = newMetadata(_inc); metadata3.getCategories().add(cat2); Metadata md3 = _repository.save(metadata3); List<Metadata> found = _repository.findAll((Specification<Metadata>)hasCategory(cat1), SortUtils.createSort(Metadata_.id)); assertEquals(2, found.size()); assertEquals(md1.getId(), found.get(0).getId()); assertEquals(md2.getId(), found.get(1).getId()); found = _repository.findAll((Specification<Metadata>)hasCategory(cat2), SortUtils.createSort(Metadata_.id)); assertEquals(2, found.size()); assertEquals(md1.getId(), found.get(0).getId()); assertEquals(md3.getId(), found.get(1).getId()); found = _repository.findAll((Specification<Metadata>)hasCategory(cat3), SortUtils.createSort(Metadata_.id)); assertEquals(1, found.size()); assertEquals(md2.getId(), found.get(0).getId()); found = _repository.findAll((Specification<Metadata>)hasCategory(cat4), SortUtils.createSort(Metadata_.id)); assertEquals(0, found.size()); }