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;
}
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;
}
Aggregations