Search in sources :

Example 6 with EntityViewManagerImpl

use of com.blazebit.persistence.view.impl.EntityViewManagerImpl in project blaze-persistence by Blazebit.

the class UnmappedBasicAttributeCascadeDeleter method removeWithoutPreCascadeDelete.

private void removeWithoutPreCascadeDelete(UpdateContext context, Object ownerId, Object[] returnedValues, Object id) {
    List<Object[]> returnedValuesList;
    // need to "return" the values from the delete query for the post deleters since the values aren't available after executing the delete query
    if (unmappedPostRemoveCascadeDeleters.length != 0 && returnedValues == null) {
        List<String> returningAttributes = new ArrayList<>();
        for (int i = 0; i < unmappedPostRemoveCascadeDeleters.length; i++) {
            returningAttributes.add(unmappedPostRemoveCascadeDeleters[i].getAttributeValuePath());
        }
        EntityViewManagerImpl evm = context.getEntityViewManager();
        // If the dbms supports it, we use the returning feature to do this
        if (evm.getDbmsDialect().supportsReturningColumns()) {
            DeleteCriteriaBuilder<?> cb = evm.getCriteriaBuilderFactory().delete(context.getEntityManager(), elementEntityClass);
            if (id == null) {
                cb.where(ownerIdAttributeName).eq(ownerId);
            } else {
                cb.where(elementIdAttributeName).eq(id);
            }
            ReturningResult<Tuple> result = cb.executeWithReturning(returningAttributes.toArray(new String[returningAttributes.size()]));
            // We make sure in the caller, that when there are post remove transition listeners, we have an id
            context.invokePostRemove(elementEntityClass, id);
            returnedValuesList = new ArrayList<>();
            for (Tuple tuple : result.getResultList()) {
                returnedValues = tuple.toArray();
                returnedValuesList.add(returnedValues);
            }
        } else {
            // Otherwise we query the attributes
            CriteriaBuilder<Object[]> cb = evm.getCriteriaBuilderFactory().create(context.getEntityManager(), Object[].class);
            cb.from(elementEntityClass);
            if (id == null) {
                cb.where(ownerIdAttributeName).eq(ownerId);
            } else {
                cb.where(elementIdAttributeName).eq(id);
            }
            for (String attribute : returningAttributes) {
                cb.select(attribute);
            }
            cb.select(elementIdAttributeName);
            returnedValuesList = new ArrayList<>();
            for (Object[] objects : cb.getResultList()) {
                returnedValues = objects;
                returnedValuesList.add(returnedValues);
                id = returnedValues[returnedValues.length - 1];
                deleteElement(context, ownerId, id);
                context.invokePostRemove(elementEntityClass, id);
            }
        }
    } else {
        deleteElement(context, ownerId, id);
        context.invokePostRemove(elementEntityClass, id);
        returnedValuesList = returnedValues == null ? Collections.<Object[]>emptyList() : Collections.singletonList(returnedValues);
    }
    for (int i = 0; i < returnedValuesList.size(); i++) {
        Object[] objects = returnedValuesList.get(i);
        for (int j = 0; j < unmappedPostRemoveCascadeDeleters.length; j++) {
            if (objects[j] != null) {
                unmappedPostRemoveCascadeDeleters[j].removeById(context, objects[j]);
            }
        }
    }
}
Also used : EntityViewManagerImpl(com.blazebit.persistence.view.impl.EntityViewManagerImpl) ArrayList(java.util.ArrayList) Tuple(javax.persistence.Tuple)

Example 7 with EntityViewManagerImpl

use of com.blazebit.persistence.view.impl.EntityViewManagerImpl 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)

Example 8 with EntityViewManagerImpl

use of com.blazebit.persistence.view.impl.EntityViewManagerImpl in project blaze-persistence by Blazebit.

the class InverseFlusher method loadByOwnerId.

public Collection<Object> loadByOwnerId(UpdateContext context, Object ownerId) {
    EntityViewManagerImpl evm = context.getEntityViewManager();
    CriteriaBuilder<?> cb = evm.getCriteriaBuilderFactory().create(context.getEntityManager(), parentEntityClass, "e");
    cb.where(parentIdAttributeName).eq(ownerId);
    cb.select("e." + attributeName + "." + childIdAttributeName);
    List<?> elementIds = cb.getResultList();
    if (elementIds.isEmpty()) {
        return Collections.emptySet();
    }
    CompositeAttributeFlusher compositeFlusher = evm.getUpdater(null, childViewToEntityMapper.getViewType(), null, null, null).getFullGraphNode();
    List<Object> elements = new ArrayList<>(elementIds.size());
    for (Object elementId : elementIds) {
        elements.add(evm.getReference(childViewToEntityMapper.getViewType().getJavaType(), compositeFlusher.createViewIdByEntityId(elementId)));
    }
    return elements;
}
Also used : EntityViewManagerImpl(com.blazebit.persistence.view.impl.EntityViewManagerImpl) ArrayList(java.util.ArrayList)

Example 9 with EntityViewManagerImpl

use of com.blazebit.persistence.view.impl.EntityViewManagerImpl in project blaze-persistence by Blazebit.

the class InverseFlusher method removeByOwnerIdOnly.

public void removeByOwnerIdOnly(UpdateContext context, Object ownerId) {
    EntityViewManagerImpl evm = context.getEntityViewManager();
    DeleteCriteriaBuilder<?> cb = evm.getCriteriaBuilderFactory().deleteCollection(context.getEntityManager(), parentEntityClass, "e", attributeName);
    cb.where(parentIdAttributeName).eq(ownerId);
    cb.executeUpdate();
}
Also used : EntityViewManagerImpl(com.blazebit.persistence.view.impl.EntityViewManagerImpl)

Aggregations

EntityViewManagerImpl (com.blazebit.persistence.view.impl.EntityViewManagerImpl)9 DeleteCriteriaBuilder (com.blazebit.persistence.DeleteCriteriaBuilder)5 Tuple (javax.persistence.Tuple)5 ArrayList (java.util.ArrayList)2 CriteriaBuilder (com.blazebit.persistence.CriteriaBuilder)1 AttributeAccessor (com.blazebit.persistence.view.impl.accessor.AttributeAccessor)1 AbstractAttribute (com.blazebit.persistence.view.impl.metamodel.AbstractAttribute)1 ManagedViewType (com.blazebit.persistence.view.metamodel.ManagedViewType)1 MapAttribute (com.blazebit.persistence.view.metamodel.MapAttribute)1 PluralAttribute (com.blazebit.persistence.view.metamodel.PluralAttribute)1 SingularAttribute (com.blazebit.persistence.view.metamodel.SingularAttribute)1 Type (com.blazebit.persistence.view.metamodel.Type)1 ViewType (com.blazebit.persistence.view.metamodel.ViewType)1