use of org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType in project spring-roo by spring-projects.
the class JpaEntityMetadataProviderImpl method getCompositionRelationField.
/**
* Gets {@link FieldMetadata} of entity field which declares a composition relationship
*
* @param entity
* @param entityDetails
* @param relationsAsChild
* @return
* @throws ClassNotFoundException
*/
private FieldMetadata getCompositionRelationField(JavaType entity, ClassOrInterfaceTypeDetails entityDetails, Map<String, FieldMetadata> relationsAsChild) throws ClassNotFoundException {
// Try to identify if it's is a child part of a composition.
// It uses details and annotation values instead metadata to
// avoid problems of circular dependencies
ClassOrInterfaceTypeDetails parentDatils;
FieldMetadata compositionRelationField = null;
AnnotationMetadata parentFieldRelationAnnotation;
JpaRelationType type;
String parentMappedBy;
for (FieldMetadata field : relationsAsChild.values()) {
parentDatils = getTypeLocationService().getTypeDetails(field.getFieldType().getBaseType());
for (FieldMetadata parentField : parentDatils.getFieldsWithAnnotation(RooJavaType.ROO_JPA_RELATION)) {
parentFieldRelationAnnotation = parentField.getAnnotation(RooJavaType.ROO_JPA_RELATION);
if (parentFieldRelationAnnotation != null && entity.equals(parentField.getFieldType().getBaseType())) {
parentMappedBy = getFieldMappedByAnnotationValue(parentField);
if (field.getFieldName().getSymbolName().equals(parentMappedBy)) {
// Found parent relation field
// Check composition
EnumDetails value = ((EnumAttributeValue) parentFieldRelationAnnotation.getAttribute(new JavaSymbolName("type"))).getValue();
if (JpaRelationType.COMPOSITION.name().equals(value.getField().getSymbolName())) {
// Found composition
if (compositionRelationField != null) {
throw new IllegalArgumentException(String.format("Found to relations which '%s' is child part of composition relation field: '%s' and '%s'", entity.getFullyQualifiedTypeName(), compositionRelationField.getFieldName(), field.getFieldName()));
}
compositionRelationField = field;
}
}
}
}
}
return compositionRelationField;
}
Aggregations