String targetAlias = extendedQuerySupport.getSqlAlias(em, baseQuery, CollectionUpdateModificationQuerySpecification.COLLECTION_BASE_QUERY_ALIAS); JoinTable joinTable = mainQuery.jpaProvider.getJoinTable(entityType, collectionName); int joinTableIndex = SqlUtils.indexOfTableName(sql, joinTable.getTableName()); String collectionAlias = SqlUtils.extractAlias(sql, joinTableIndex + joinTable.getTableName().length()); String updateSql = "update " + joinTable.getTableName() + " set "; columnExpressionRemappings.put(ownerAlias + "." + discriminatorColumnCheck[0] + "=" + discriminatorColumnCheck[1], "1=1"); if (joinTable.getKeyColumnMappings() != null) { for (Map.Entry<String, String> entry : joinTable.getKeyColumnMappings().entrySet()) { columnOnlyRemappings.put(collectionAlias + "." + entry.getValue(), entry.getKey()); columnExpressionRemappings.put(collectionAlias + "." + entry.getValue(), joinTable.getTableName() + "." + entry.getKey()); for (Map.Entry<String, String> entry : joinTable.getIdColumnMappings().entrySet()) { columnOnlyRemappings.put(ownerAlias + "." + entry.getValue(), entry.getKey()); columnExpressionRemappings.put(ownerAlias + "." + entry.getValue(), joinTable.getTableName() + "." + entry.getKey()); for (Map.Entry<String, String> entry : joinTable.getTargetColumnMappings().entrySet()) { columnOnlyRemappings.put(targetAlias + "." + entry.getValue(), entry.getKey()); columnExpressionRemappings.put(targetAlias + "." + entry.getValue(), joinTable.getTableName() + "." + entry.getKey());
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("No support for inserting into inverse collection via DML API yet!"); } 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; }
columnTypes = entry.getColumnTypes(); } else { Collection<String> types = entry.getJoinTable().getKeyColumnTypes().values(); columnTypes = types.toArray(new String[types.size()]);
throw new IllegalStateException("Deleting inverse collections is not supported!"); int joinTableIndex = SqlUtils.indexOfTableName(sql, joinTable.getTableName()); String collectionAlias = SqlUtils.extractAlias(sql, joinTableIndex + joinTable.getTableName().length()); String deleteSql = "delete from " + joinTable.getTableName(); Map<String, String> columnExpressionRemappings = new HashMap<>(joinTable.getIdColumnMappings().size()); columnExpressionRemappings.put(collectionAlias + ".", joinTable.getTableName() + "."); columnExpressionRemappings.put(ownerAlias + "." + discriminatorColumnCheck[0] + "=" + discriminatorColumnCheck[1], "1=1"); for (Map.Entry<String, String> entry : joinTable.getIdColumnMappings().entrySet()) { columnExpressionRemappings.put(ownerAlias + "." + entry.getValue(), joinTable.getTableName() + "." + entry.getKey()); for (Map.Entry<String, String> entry : joinTable.getTargetColumnMappings().entrySet()) { columnExpressionRemappings.put(targetAlias + "." + entry.getValue(), joinTable.getTableName() + "." + entry.getKey());
return new JoinTable( queryableCollection.getTableName(), idAttributeNames,
protected void clearCollections(EntityManager em, Class<?>... entityClasses) { for (Class<?> c : entityClasses) { List<String> deletes = PLURAL_DELETES.get(c); if (deletes == null) { Metamodel entityMetamodel = cbf.getService(Metamodel.class); EntityType<?> t = entityMetamodel.entity(c); deletes = new ArrayList<>(); for (PluralAttribute<?, ?, ?> pluralAttribute : t.getPluralAttributes()) { JoinTable joinTable = jpaProvider.getJoinTable(t, pluralAttribute.getName()); if (joinTable != null) { deletes.add("delete from " + joinTable.getTableName()); } } PLURAL_DELETES.put(c, deletes); } for (String delete : deletes) { em.createNativeQuery(delete).executeUpdate(); } } }
if (attributeEntry != null && attributeEntry.joinTable != null && attributeEntry.joinTable.getTargetAttributeNames() != null) { for (String targetAttributeName : attributeEntry.joinTable.getTargetAttributeNames()) { String subAttributeName = attributeName; ManagedType<?> subType = delegate.managedType(attributeEntry.getElementClass());
protected boolean determineForcedUnique(MetamodelBuildingContext context) { if (isCollection() && getMapping() != null && getMapping().indexOf('.') == -1) { ExtendedManagedType<?> managedType = context.getEntityMetamodel().getManagedType(ExtendedManagedType.class, getDeclaringType().getJpaManagedType()); ExtendedAttribute<?, ?> attribute = managedType.getOwnedAttributes().get(getMapping()); if (attribute != null && attribute.getAttribute() instanceof javax.persistence.metamodel.PluralAttribute<?, ?, ?>) { // TODO: we should add that information to ExtendedAttribute return (((javax.persistence.metamodel.PluralAttribute<?, ?, ?>) attribute.getAttribute()).getCollectionType() != javax.persistence.metamodel.PluralAttribute.CollectionType.MAP) && (!StringUtils.isEmpty(attribute.getMappedBy()) || !attribute.isBag()) && (attribute.getJoinTable() == null || attribute.getJoinTable().getKeyColumnMappings() == null) && !MetamodelUtils.isIndexedList(context.getEntityMetamodel(), context.getExpressionFactory(), managedType.getType().getJavaType(), getMapping()); } } return false; }
Map<String, String> keyColumnTypes = null; Map<String, String> targetIdColumnMapping = new HashMap<>(); return new JoinTable( oneToOneMapping.getRelationTable().getName(), null, return new JoinTable( manyToManyMapping.getRelationTable().getName(), null, idColumnMapping.put(sourceKeyFields.get(i).getName(), referenceKeyFields.get(i).getName()); return new JoinTable( directCollectionMapping.getReferenceTableName(), null, idColumnMapping.put(sourceKeyFields.get(i).getName(), referenceKeyFields.get(i).getName()); return new JoinTable( directMapMapping.getReferenceTableName(), null, idColumnMapping.put(sourceKeyFields.get(i).getName(), targetForeignKeyFields.get(i).getName()); return new JoinTable( tableName, null,
protected void clearCollections(EntityManager em, Class<?>... entityClasses) { for (Class<?> c : entityClasses) { List<String> deletes = PLURAL_DELETES.get(c); if (deletes == null) { Metamodel entityMetamodel = cbf.getService(Metamodel.class); EntityType<?> t = entityMetamodel.entity(c); deletes = new ArrayList<>(); for (PluralAttribute<?, ?, ?> pluralAttribute : t.getPluralAttributes()) { JoinTable joinTable = jpaProvider.getJoinTable(t, pluralAttribute.getName()); if (joinTable != null) { deletes.add("delete from " + joinTable.getTableName()); } } PLURAL_DELETES.put(c, deletes); } for (String delete : deletes) { em.createNativeQuery(delete).executeUpdate(); } } }
@Override public List<String> getIdentifierOrUniqueKeyEmbeddedPropertyNames(EntityType<?> owner, String attributeName) { AbstractEntityPersister entityPersister = getEntityPersister(owner); Type propertyType = entityPersister.getPropertyType(attributeName); List<String> identifierOrUniqueKeyPropertyNames = new ArrayList<>(); if (propertyType instanceof CollectionType) { Type elementType = ((CollectionType) propertyType).getElementType(entityPersister.getFactory()); Collection<String> targetAttributeNames = getJoinTable(owner, attributeName).getTargetAttributeNames(); if (targetAttributeNames == null) { collectPropertyNames(identifierOrUniqueKeyPropertyNames, null, elementType, entityPersister.getFactory()); } else { AbstractEntityPersister elementPersister = (AbstractEntityPersister) entityPersisters.get(((org.hibernate.type.EntityType) elementType).getAssociatedEntityName()); for (String targetAttributeName : targetAttributeNames) { collectPropertyNames(identifierOrUniqueKeyPropertyNames, targetAttributeName, elementPersister.getPropertyType(targetAttributeName), entityPersister.getFactory()); } } } else { collectPropertyNames(identifierOrUniqueKeyPropertyNames, null, propertyType, entityPersister.getFactory()); } return identifierOrUniqueKeyPropertyNames; }
String targetAlias = extendedQuerySupport.getSqlAlias(em, insertExampleQuery, COLLECTION_BASE_QUERY_ALIAS); JoinTable joinTable = mainQuery.jpaProvider.getJoinTable(entityType, collectionName); int joinTableIndex = SqlUtils.indexOfTableName(insertExampleSql, joinTable.getTableName()); String collectionAlias = SqlUtils.extractAlias(insertExampleSql, joinTableIndex + joinTable.getTableName().length()); String[] selectItemExpressions = SqlUtils.getSelectItemExpressions(insertExampleSql, SqlUtils.indexOfSelect(insertExampleSql)); columnExpressionRemappings.put(ownerAlias + "." + discriminatorColumnCheck[0] + "=" + discriminatorColumnCheck[1], "1=1"); if (joinTable.getKeyColumnMappings() != null) { for (Map.Entry<String, String> entry : joinTable.getKeyColumnMappings().entrySet()) { columnExpressionRemappings.put(collectionAlias + "." + entry.getValue(), entry.getKey()); for (Map.Entry<String, String> entry : joinTable.getIdColumnMappings().entrySet()) { columnExpressionRemappings.put(ownerAlias + "." + entry.getValue(), entry.getKey()); for (Map.Entry<String, String> entry : joinTable.getTargetColumnMappings().entrySet()) { columnExpressionRemappings.put(targetAlias + "." + entry.getValue(), entry.getKey()); insertSqlSb.append("insert into ").append(joinTable.getTableName()).append("("); for (String selectItemExpression : selectItemExpressions) { String columnExpression = columnExpressionRemappings.get(selectItemExpression.trim());
Map<String, String> keyColumnTypes = null; Map<String, String> targetIdColumnMapping = new HashMap<>(); return new JoinTable( oneToOneMapping.getRelationTable().getName(), null, return new JoinTable( manyToManyMapping.getRelationTable().getName(), null, idColumnMapping.put(sourceKeyFields.get(i).getName(), referenceKeyFields.get(i).getName()); return new JoinTable( directCollectionMapping.getReferenceTableName(), null, idColumnMapping.put(sourceKeyFields.get(i).getName(), referenceKeyFields.get(i).getName()); return new JoinTable( directMapMapping.getReferenceTableName(), null, idColumnMapping.put(sourceKeyFields.get(i).getName(), targetForeignKeyFields.get(i).getName()); return new JoinTable( tableName, null,
@Override public String tableFromEntityRelation(Class<?> entityClass, String relationName) { JoinTable joinTable = jpaProvider.getJoinTable(em.getMetamodel().entity(entityClass), relationName); if (joinTable != null) { return joinTable.getTableName(); } return null; } };
Map<String, String> keyColumnTypes = valuesLikeAttribute.getJoinTable().getKeyColumnTypes(); if (keyColumnTypes.size() != 1) { throw new IllegalArgumentException("Unsupported VALUES clause use with multi-column attribute type " + keyColumnTypes.values() + "! Consider creating a synthetic type like a @CTE entity to hold this attribute and use that type via fromIdentifiableValues instead!");
return new JoinTable( tableName, null,
return new JoinTable( tableName, null,
return new JoinTable( tableName, null,