Search in sources :

Example 1 with DeleteCriteriaBuilder

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

the class MapAttributeFlusher method removeByOwnerId.

private List<PostFlushDeleter> removeByOwnerId(UpdateContext context, Object ownerId, boolean cascade) {
    EntityViewManagerImpl evm = context.getEntityViewManager();
    if (cascade) {
        List<Object> elementIds;
        // If there is no inverseFlusher/mapped by attribute, the collection has a join table
        if (evm.getDbmsDialect().supportsReturningColumns()) {
            List<Tuple> tuples = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", mapping).where(ownerIdAttributeName).eq(ownerId).executeWithReturning(mapping + "." + elementDescriptor.getAttributeIdAttributeName()).getResultList();
            elementIds = new ArrayList<>(tuples.size());
            for (Tuple tuple : tuples) {
                elementIds.add(tuple.get(0));
            }
        } else {
            elementIds = (List<Object>) evm.getCriteriaBuilderFactory().create(context.getEntityManager(), ownerEntityClass, "e").where(ownerIdAttributeName).eq(ownerId).select("e." + mapping + "." + elementDescriptor.getAttributeIdAttributeName()).getResultList();
            if (!elementIds.isEmpty() && !jpaProviderDeletesCollection) {
                // We must always delete this, otherwise we might get a constraint violation because of the cascading delete
                DeleteCriteriaBuilder<?> cb = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", mapping);
                cb.where(ownerIdAttributeName).eq(ownerId);
                cb.executeUpdate();
            }
        }
        return Collections.<PostFlushDeleter>singletonList(new PostFlushCollectionElementByIdDeleter(elementDescriptor.getElementToEntityMapper(), elementIds));
    } else if (!jpaProviderDeletesCollection) {
        // delete from Entity(collectionRole) e where e.id = :id
        DeleteCriteriaBuilder<?> cb = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", mapping);
        cb.where("e." + ownerIdAttributeName).eq(ownerId);
        cb.executeUpdate();
    }
    return Collections.emptyList();
}
Also used : EntityViewManagerImpl(com.blazebit.persistence.view.impl.EntityViewManagerImpl) DeleteCriteriaBuilder(com.blazebit.persistence.DeleteCriteriaBuilder) Tuple(javax.persistence.Tuple)

Example 2 with DeleteCriteriaBuilder

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

the class UnmappedCollectionAttributeCascadeDeleter method removeByOwnerId.

@Override
public void removeByOwnerId(UpdateContext context, Object ownerId) {
    EntityViewManagerImpl evm = context.getEntityViewManager();
    if (cascadeDeleteElement) {
        List<Object> elementIds;
        if (mappedByAttributeName == null) {
            // If there is no mapped by attribute, the collection has a join table
            if (evm.getDbmsDialect().supportsReturningColumns()) {
                List<Tuple> tuples = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", attributeName).where(ownerIdAttributeName).eq(ownerId).executeWithReturning(attributeName + "." + elementIdAttributeName).getResultList();
                elementIds = new ArrayList<>(tuples.size());
                for (Tuple tuple : tuples) {
                    elementIds.add(tuple.get(0));
                }
            } else {
                elementIds = (List<Object>) evm.getCriteriaBuilderFactory().create(context.getEntityManager(), ownerEntityClass, "e").where(ownerIdAttributeName).eq(ownerId).select("e." + attributeName + "." + elementIdAttributeName).getResultList();
                if (!elementIds.isEmpty()) {
                    // We must always delete this, otherwise we might get a constraint violation because of the cascading delete
                    DeleteCriteriaBuilder<?> cb = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", attributeName);
                    cb.where(ownerIdAttributeName).eq(ownerId);
                    cb.executeUpdate();
                }
            }
            for (Object elementId : elementIds) {
                elementDeleter.removeById(context, elementId);
            }
        } else {
            // Since there is a mapped by attribute, there is no join table to clear. Just delete the element by the owner id
            elementDeleter.removeByOwnerId(context, ownerId);
        }
    } else if (!jpaProviderDeletesCollection) {
        DeleteCriteriaBuilder<?> cb = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", attributeName);
        cb.where(ownerIdAttributeName).eq(ownerId);
        cb.executeUpdate();
    }
}
Also used : EntityViewManagerImpl(com.blazebit.persistence.view.impl.EntityViewManagerImpl) DeleteCriteriaBuilder(com.blazebit.persistence.DeleteCriteriaBuilder) Tuple(javax.persistence.Tuple)

Example 3 with DeleteCriteriaBuilder

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

the class CollectionAttributeFlusher method removeByOwnerId.

private List<PostFlushDeleter> removeByOwnerId(UpdateContext context, Object ownerId, boolean cascade) {
    EntityViewManagerImpl evm = context.getEntityViewManager();
    String mapping = getMapping();
    if (cascade) {
        List<Object> elementIds;
        if (inverseFlusher == null) {
            // If there is no inverseFlusher/mapped by attribute, the collection has a join table
            if (evm.getDbmsDialect().supportsReturningColumns()) {
                List<Tuple> tuples = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", mapping).where(ownerIdAttributeName).eq(ownerId).executeWithReturning(mapping + "." + elementDescriptor.getAttributeIdAttributeName()).getResultList();
                elementIds = new ArrayList<>(tuples.size());
                for (Tuple tuple : tuples) {
                    elementIds.add(tuple.get(0));
                }
            } else {
                elementIds = (List<Object>) evm.getCriteriaBuilderFactory().create(context.getEntityManager(), ownerEntityClass, "e").where(ownerIdAttributeName).eq(ownerId).where("e." + mapping + "." + elementDescriptor.getAttributeIdAttributeName()).isNotNull().select("e." + mapping + "." + elementDescriptor.getAttributeIdAttributeName()).getResultList();
                if (!elementIds.isEmpty() && !jpaProviderDeletesCollection) {
                    // We must always delete this, otherwise we might get a constraint violation because of the cascading delete
                    DeleteCriteriaBuilder<?> cb = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", mapping);
                    cb.where(ownerIdAttributeName).eq(ownerId);
                    cb.executeUpdate();
                }
            }
            return Collections.<PostFlushDeleter>singletonList(new PostFlushCollectionElementByIdDeleter(elementDescriptor.getElementToEntityMapper(), elementIds));
        } else {
            return inverseFlusher.removeByOwnerId(context, ownerId);
        }
    } else if (!jpaProviderDeletesCollection) {
        // delete from Entity(collectionRole) e where e.id = :id
        DeleteCriteriaBuilder<?> cb = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", mapping);
        cb.where(ownerIdAttributeName).eq(ownerId);
        cb.executeUpdate();
    }
    return Collections.emptyList();
}
Also used : EntityViewManagerImpl(com.blazebit.persistence.view.impl.EntityViewManagerImpl) DeleteCriteriaBuilder(com.blazebit.persistence.DeleteCriteriaBuilder) Tuple(javax.persistence.Tuple)

Example 4 with DeleteCriteriaBuilder

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

the class UnmappedMapAttributeCascadeDeleter method removeByOwnerId.

@Override
public void removeByOwnerId(UpdateContext context, Object ownerId) {
    EntityViewManagerImpl evm = context.getEntityViewManager();
    if (cascadeDeleteElement) {
        List<Object> elementIds;
        if (evm.getDbmsDialect().supportsReturningColumns()) {
            List<Tuple> tuples = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", attributeName).where(ownerIdAttributeName).eq(ownerId).executeWithReturning(attributeName + "." + elementIdAttributeName).getResultList();
            elementIds = new ArrayList<>(tuples.size());
            for (Tuple tuple : tuples) {
                elementIds.add(tuple.get(0));
            }
        } else {
            elementIds = (List<Object>) evm.getCriteriaBuilderFactory().create(context.getEntityManager(), ownerEntityClass, "e").where(ownerIdAttributeName).eq(ownerId).select("e." + attributeName + "." + elementIdAttributeName).getResultList();
            if (!elementIds.isEmpty()) {
                // We must always delete this, otherwise we might get a constraint violation because of the cascading delete
                DeleteCriteriaBuilder<?> cb = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", attributeName);
                cb.where(ownerIdAttributeName).eq(ownerId);
                cb.executeUpdate();
            }
        }
        for (Object elementId : elementIds) {
            elementDeleter.removeById(context, elementId);
        }
    } else if (!jpaProviderDeletesCollection) {
        DeleteCriteriaBuilder<?> cb = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), ownerEntityClass, "e", attributeName);
        cb.where(ownerIdAttributeName).eq(ownerId);
        cb.executeUpdate();
    }
}
Also used : EntityViewManagerImpl(com.blazebit.persistence.view.impl.EntityViewManagerImpl) DeleteCriteriaBuilder(com.blazebit.persistence.DeleteCriteriaBuilder) Tuple(javax.persistence.Tuple)

Aggregations

DeleteCriteriaBuilder (com.blazebit.persistence.DeleteCriteriaBuilder)4 EntityViewManagerImpl (com.blazebit.persistence.view.impl.EntityViewManagerImpl)4 Tuple (javax.persistence.Tuple)4