private void validateEntityValueReferences(Entity entity, ValidationResource validationResource) { validationResource .getRefAttrs() .forEach( refAttr -> { HugeSet<Object> refEntityIds = validationResource.getRefEntitiesIds().get(refAttr.getRefEntity().getName()); Iterable<Entity> refEntities; if (refAttr.getDataType() instanceof XrefField) { Entity refEntity = entity.getEntity(refAttr.getName()); if (refEntity != null) { refEntities = singleton(refEntity); } else { refEntities = emptyList(); } } else { refEntities = entity.getEntities(refAttr.getName()); } for (Entity refEntity : refEntities) { if (!refEntityIds.contains(refEntity.getIdValue())) { boolean selfReference = entity.getEntityMetaData().getName().equals(refAttr.getRefEntity().getName()); if (!(selfReference && entity.getIdValue().equals(refEntity.getIdValue()))) { String message = String.format( "Unknown xref value '%s' for attribute '%s' of entity '%s'.", DataConverter.toString(refEntity.getIdValue()), refAttr.getName(), getName()); ConstraintViolation constraintViolation = new ConstraintViolation( message, refAttr, Long.valueOf(validationResource.getRow())); validationResource.addViolation(constraintViolation); } } validationResource.addRefEntityId(getName(), refEntity.getIdValue()); } }); }