Search in sources :

Example 1 with JpaRelationType

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;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) EnumDetails(org.springframework.roo.model.EnumDetails) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) JpaRelationType(org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

JpaRelationType (org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType)1 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)1 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)1 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)1 EnumAttributeValue (org.springframework.roo.classpath.details.annotations.EnumAttributeValue)1 EnumDetails (org.springframework.roo.model.EnumDetails)1 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)1