Example usage for javax.persistence.metamodel SingularAttribute getDeclaringType

List of usage examples for javax.persistence.metamodel SingularAttribute getDeclaringType

Introduction

In this page you can find the example usage for javax.persistence.metamodel SingularAttribute getDeclaringType.

Prototype

ManagedType<X> getDeclaringType();

Source Link

Document

Return the managed type representing the type in which the attribute was declared.

Usage

From source file:org.kuali.rice.krad.data.jpa.eclipselink.EclipseLinkJpaMetadataProviderImpl.java

/**
 * {@inheritDoc}// w  w w.j a v  a  2  s.  c om
 */
@Override
protected void populateImplementationSpecificRelationshipLevelMetadata(DataObjectRelationshipImpl relationship,
        SingularAttribute<?, ?> rd) {
    // We need to go into the repository and grab the table name.
    Class<?> referencedClass = rd.getBindableJavaType();
    EntityType<?> referencedEntityType = entityManager.getMetamodel().entity(referencedClass);
    if (referencedEntityType instanceof EntityTypeImpl) {
        relationship.setBackingObjectName(
                ((EntityTypeImpl<?>) referencedEntityType).getDescriptor().getTableName());
    }
    // Set to read only if store (save) operations should not be pushed through
    PersistentAttributeType persistentAttributeType = rd.getPersistentAttributeType();

    if (rd instanceof SingularAttributeImpl) {
        SingularAttributeImpl<?, ?> rel = (SingularAttributeImpl<?, ?>) rd;

        OneToOneMapping relationshipMapping = (OneToOneMapping) rel.getMapping();
        relationship.setReadOnly(relationshipMapping.isReadOnly());
        relationship.setSavedWithParent(relationshipMapping.isCascadePersist());
        relationship.setDeletedWithParent(relationshipMapping.isCascadeRemove());
        relationship.setLoadedAtParentLoadTime(
                relationshipMapping.isCascadeRefresh() && !relationshipMapping.isLazy());
        relationship.setLoadedDynamicallyUponUse(
                relationshipMapping.isCascadeRefresh() && relationshipMapping.isLazy());

        List<DataObjectAttributeRelationship> attributeRelationships = new ArrayList<DataObjectAttributeRelationship>();
        for (DatabaseField parentField : relationshipMapping.getForeignKeyFields()) {
            String parentFieldName = getPropertyNameFromDatabaseColumnName(rd.getDeclaringType(),
                    parentField.getName());
            if (parentFieldName != null) {
                DatabaseField childField = relationshipMapping.getSourceToTargetKeyFields().get(parentField);
                if (childField != null) {
                    // the target field is always done by column name. So, we need to get into the target entity and
                    // find the associated field :-(
                    // If the lookup fails, we will at least have the column name
                    String childFieldName = getPropertyNameFromDatabaseColumnName(referencedEntityType,
                            childField.getName());
                    if (childFieldName != null) {
                        attributeRelationships
                                .add(new DataObjectAttributeRelationshipImpl(parentFieldName, childFieldName));
                    }
                } else {
                    LOG.warn("Unable to find child field reference.  There may be a JPA mapping problem on "
                            + rd.getDeclaringType().getJavaType() + ": " + relationship);
                }
            }
        }
        relationship.setAttributeRelationships(attributeRelationships);

        populateInverseRelationship(relationshipMapping, relationship);

    } else {
        // get what we can based on JPA values (note that we just set some to have values here)
        relationship.setReadOnly(persistentAttributeType == PersistentAttributeType.MANY_TO_ONE);
        relationship.setSavedWithParent(persistentAttributeType == PersistentAttributeType.ONE_TO_ONE);
        relationship.setDeletedWithParent(persistentAttributeType == PersistentAttributeType.ONE_TO_ONE);
        relationship.setLoadedAtParentLoadTime(true);
        relationship.setLoadedDynamicallyUponUse(false);
    }
}