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