Search in sources :

Example 11 with JoinTable

use of com.blazebit.persistence.spi.JoinTable in project blaze-persistence by Blazebit.

the class EclipseLinkJpaProvider method getJoinTable.

@Override
public JoinTable getJoinTable(EntityType<?> ownerType, String attributeName) {
    DatabaseMapping mapping = getAttribute(ownerType, attributeName).getMapping();
    if (mapping instanceof OneToOneMapping) {
        OneToOneMapping oneToOneMapping = (OneToOneMapping) mapping;
        if (oneToOneMapping.hasRelationTable()) {
            Map<String, String> idColumnMapping = new LinkedHashMap<>();
            Map<String, String> keyMapping = null;
            Map<String, String> keyColumnTypes = null;
            Map<String, String> targetIdColumnMapping = new LinkedHashMap<>();
            return new JoinTable(oneToOneMapping.getRelationTable().getName(), null, idColumnMapping, keyMapping, keyColumnTypes, null, targetIdColumnMapping);
        }
    } else if (mapping instanceof CollectionMapping) {
        CollectionMapping collectionMapping = (CollectionMapping) mapping;
        if (collectionMapping instanceof ManyToManyMapping) {
            ManyToManyMapping manyToManyMapping = (ManyToManyMapping) collectionMapping;
            Vector<DatabaseField> sourceKeyFields = manyToManyMapping.getSourceKeyFields();
            Vector<DatabaseField> sourceRelationKeyFields = manyToManyMapping.getSourceRelationKeyFields();
            Vector<DatabaseField> targetKeyFields = manyToManyMapping.getTargetKeyFields();
            Vector<DatabaseField> targetRelationKeyFields = manyToManyMapping.getTargetRelationKeyFields();
            Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
            Map<String, String> targetIdColumnMapping = new LinkedHashMap<>(targetKeyFields.size());
            for (int i = 0; i < sourceKeyFields.size(); i++) {
                idColumnMapping.put(sourceKeyFields.get(i).getName(), sourceRelationKeyFields.get(i).getName());
            }
            for (int i = 0; i < targetKeyFields.size(); i++) {
                targetIdColumnMapping.put(targetKeyFields.get(i).getName(), targetRelationKeyFields.get(i).getName());
            }
            return new JoinTable(manyToManyMapping.getRelationTable().getName(), null, idColumnMapping, keyMapping(manyToManyMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
        } else if (collectionMapping instanceof DirectCollectionMapping) {
            DirectCollectionMapping directCollectionMapping = (DirectCollectionMapping) collectionMapping;
            Vector<DatabaseField> sourceKeyFields = directCollectionMapping.getSourceKeyFields();
            Vector<DatabaseField> referenceKeyFields = directCollectionMapping.getReferenceKeyFields();
            Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
            Map<String, String> targetIdColumnMapping = Collections.emptyMap();
            for (int i = 0; i < sourceKeyFields.size(); i++) {
                idColumnMapping.put(sourceKeyFields.get(i).getName(), referenceKeyFields.get(i).getName());
            }
            return new JoinTable(directCollectionMapping.getReferenceTableName(), null, idColumnMapping, keyMapping(directCollectionMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
        } else if (collectionMapping instanceof DirectMapMapping) {
            DirectMapMapping directMapMapping = (DirectMapMapping) collectionMapping;
            Vector<DatabaseField> sourceKeyFields = directMapMapping.getSourceKeyFields();
            Vector<DatabaseField> referenceKeyFields = directMapMapping.getReferenceKeyFields();
            Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
            Map<String, String> targetIdColumnMapping = Collections.emptyMap();
            for (int i = 0; i < sourceKeyFields.size(); i++) {
                idColumnMapping.put(sourceKeyFields.get(i).getName(), referenceKeyFields.get(i).getName());
            }
            return new JoinTable(directMapMapping.getReferenceTableName(), null, idColumnMapping, keyMapping(directMapMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
        } else if (collectionMapping instanceof AggregateCollectionMapping) {
            AggregateCollectionMapping aggregateCollectionMapping = (AggregateCollectionMapping) collectionMapping;
            Vector<DatabaseField> sourceKeyFields = aggregateCollectionMapping.getSourceKeyFields();
            Vector<DatabaseField> targetForeignKeyFields = aggregateCollectionMapping.getTargetForeignKeyFields();
            Map<String, String> idColumnMapping = new LinkedHashMap<>(sourceKeyFields.size());
            Map<String, String> targetIdColumnMapping = Collections.emptyMap();
            String tableName = null;
            for (int i = 0; i < sourceKeyFields.size(); i++) {
                tableName = targetForeignKeyFields.get(i).getTableName();
                idColumnMapping.put(sourceKeyFields.get(i).getName(), targetForeignKeyFields.get(i).getName());
            }
            return new JoinTable(tableName, null, idColumnMapping, keyMapping(aggregateCollectionMapping.getContainerPolicy().getIdentityFieldsForMapKey()), null, null, targetIdColumnMapping);
        }
    }
    return null;
}
Also used : ManyToManyMapping(org.eclipse.persistence.mappings.ManyToManyMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) LinkedHashMap(java.util.LinkedHashMap) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping) DirectMapMapping(org.eclipse.persistence.mappings.DirectMapMapping) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) DirectCollectionMapping(org.eclipse.persistence.mappings.DirectCollectionMapping) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping) AggregateCollectionMapping(org.eclipse.persistence.mappings.AggregateCollectionMapping) Vector(java.util.Vector) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JoinTable(com.blazebit.persistence.spi.JoinTable)

Example 12 with JoinTable

use of com.blazebit.persistence.spi.JoinTable in project blaze-persistence by Blazebit.

the class JpaUtils method getCollectionAttributeEntries.

public static Map<String, ExtendedAttribute<?, ?>> getCollectionAttributeEntries(EntityMetamodel metamodel, EntityType<?> entityType, ExtendedAttribute<?, ?> attribute) {
    Map<String, ExtendedAttribute<?, ?>> collectionAttributeEntries = new HashMap<>();
    JoinTable joinTable = attribute.getJoinTable();
    if (joinTable == null) {
        throw new IllegalArgumentException("Inserting into or updating an inverse collection via DML API is not supported!");
    }
    ExtendedManagedType<?> extendedManagedType = metamodel.getManagedType(ExtendedManagedType.class, entityType);
    for (String idAttributeName : joinTable.getIdAttributeNames()) {
        collectionAttributeEntries.put(idAttributeName, extendedManagedType.getAttribute(idAttributeName));
    }
    if (((PluralAttribute<?, ?, ?>) attribute.getAttribute()).getElementType() instanceof ManagedType<?>) {
        String prefix = attribute.getAttributePathString() + ".";
        for (Map.Entry<String, ? extends ExtendedAttribute<?, ?>> entry : extendedManagedType.getAttributes().entrySet()) {
            if (entry.getKey().startsWith(prefix)) {
                collectionAttributeEntries.put(entry.getKey(), entry.getValue());
            }
        }
    }
    collectionAttributeEntries.put(attribute.getAttributePathString(), attribute);
    return collectionAttributeEntries;
}
Also used : ExtendedManagedType(com.blazebit.persistence.spi.ExtendedManagedType) ManagedType(javax.persistence.metamodel.ManagedType) HashMap(java.util.HashMap) ExtendedAttribute(com.blazebit.persistence.spi.ExtendedAttribute) HashMap(java.util.HashMap) Map(java.util.Map) JoinTable(com.blazebit.persistence.spi.JoinTable)

Aggregations

JoinTable (com.blazebit.persistence.spi.JoinTable)12 ArrayList (java.util.ArrayList)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 HashMap (java.util.HashMap)4 ReturningBuilder (com.blazebit.persistence.ReturningBuilder)3 CTENode (com.blazebit.persistence.impl.query.CTENode)3 ExtendedQuerySupport (com.blazebit.persistence.spi.ExtendedQuerySupport)3 ManagedType (javax.persistence.metamodel.ManagedType)3 SingularAttribute (javax.persistence.metamodel.SingularAttribute)3 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)3 JoinType (com.blazebit.persistence.JoinType)2 ExtendedAttribute (com.blazebit.persistence.spi.ExtendedAttribute)2 ExtendedManagedType (com.blazebit.persistence.spi.ExtendedManagedType)2 EntityType (javax.persistence.metamodel.EntityType)2 IdentifiableType (javax.persistence.metamodel.IdentifiableType)2 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)2 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)2 KeyMetaData (org.datanucleus.metadata.KeyMetaData)2 AssociationType (org.hibernate.type.AssociationType)2