MetadataValidationSpecs { public static Specification<MetadataValidation> isInvalidAndRequiredForMetadata(final int metadataId) { return new Specification<MetadataValidation>() { @Override public Predicate toPredicate(Root<MetadataValidation> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Path<MetadataValidationStatus> statusAttributePath = root.get(MetadataValidation_.status); Path<Boolean> requiredAttributePath = root.get(MetadataValidation_.required); Path<Integer> metadataIdAttributePath = root.get(MetadataValidation_.id).get(MetadataValidationId_.metadataId); return cb.and(cb.equal(metadataIdAttributePath, cb.literal(metadataId)), cb.and(cb.equal(statusAttributePath, cb.literal(MetadataValidationStatus.INVALID)), cb.equal(requiredAttributePath, cb.literal(Boolean.TRUE)))); } }; } private  MetadataValidationSpecs(); static Specification<MetadataValidation> isInvalidAndRequiredForMetadata(final int metadataId); static Specification<MetadataValidation> hasMetadataId(final int metadataId);  }
@Test public void testIsInvalidAndRequiredForMetadata() throws Exception { MetadataValidation md1 = _metadataValidationRepo.save(newMetadataValidation()); md1.setValid(true); _metadataValidationRepo.save(md1); MetadataValidation md2 = _metadataValidationRepo.save(newMetadataValidation()); md2.setValid(false); _metadataValidationRepo.save(md2); List<MetadataValidation> found = _metadataValidationRepo.findAll(isInvalidAndRequiredForMetadata(md1.getId().getMetadataId())); assertEquals(0, found.size()); found = _metadataValidationRepo.findAll(isInvalidAndRequiredForMetadata(md2.getId().getMetadataId())); assertEquals(1, found.size()); }