Search in sources :

Example 1 with AbstractCollectionPersister

use of org.hibernate.persister.collection.AbstractCollectionPersister in project hibernate-orm by hibernate.

the class AbstractInlineIdsDeleteHandlerImpl method execute.

@Override
public int execute(SharedSessionContractImplementor session, QueryParameters queryParameters) {
    IdsClauseBuilder values = prepareInlineStatement(session, queryParameters);
    if (!values.getIds().isEmpty()) {
        final String idSubselect = values.toStatement();
        for (Type type : getTargetedQueryable().getPropertyTypes()) {
            if (type.isCollectionType()) {
                CollectionType cType = (CollectionType) type;
                AbstractCollectionPersister cPersister = (AbstractCollectionPersister) factory().getMetamodel().collectionPersister(cType.getRole());
                if (cPersister.isManyToMany()) {
                    deletes.add(generateDelete(cPersister.getTableName(), cPersister.getKeyColumnNames(), idSubselect, "bulk delete - m2m join table cleanup").toStatementString());
                }
            }
        }
        String[] tableNames = getTargetedQueryable().getConstraintOrderedTableNameClosure();
        String[][] columnNames = getTargetedQueryable().getContraintOrderedTableKeyColumnClosure();
        for (int i = 0; i < tableNames.length; i++) {
            // TODO : an optimization here would be to consider cascade deletes and not gen those delete statements;
            // the difficulty is the ordering of the tables here vs the cascade attributes on the persisters ->
            // the table info gotten here should really be self-contained (i.e., a class representation
            // defining all the needed attributes), then we could then get an array of those
            deletes.add(generateDelete(tableNames[i], columnNames[i], idSubselect, "bulk delete").toStatementString());
        }
        // Start performing the deletes
        for (String delete : deletes) {
            if (delete == null) {
                continue;
            }
            try {
                try (PreparedStatement ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(delete, false)) {
                    session.getJdbcCoordinator().getResultSetReturn().executeUpdate(ps);
                }
            } catch (SQLException e) {
                throw convert(e, "error performing bulk delete", delete);
            }
        }
    }
    return values.getIds().size();
}
Also used : CollectionType(org.hibernate.type.CollectionType) Type(org.hibernate.type.Type) SQLException(java.sql.SQLException) CollectionType(org.hibernate.type.CollectionType) PreparedStatement(java.sql.PreparedStatement) AbstractCollectionPersister(org.hibernate.persister.collection.AbstractCollectionPersister)

Example 2 with AbstractCollectionPersister

use of org.hibernate.persister.collection.AbstractCollectionPersister in project hibernate-orm by hibernate.

the class BaseEnversCollectionEventListener method onCollectionActionInversed.

protected final void onCollectionActionInversed(AbstractCollectionEvent event, PersistentCollection newColl, Serializable oldColl, CollectionEntry collectionEntry) {
    if (shouldGenerateRevision(event)) {
        final String entityName = event.getAffectedOwnerEntityName();
        final String ownerEntityName = ((AbstractCollectionPersister) collectionEntry.getLoadedPersister()).getOwnerEntityName();
        final String referencingPropertyName = collectionEntry.getRole().substring(ownerEntityName.length() + 1);
        final RelationDescription rd = searchForRelationDescription(entityName, referencingPropertyName);
        if (rd != null) {
            if (rd.getRelationType().equals(RelationType.TO_MANY_NOT_OWNING) && rd.isIndexed()) {
                onCollectionAction(event, newColl, oldColl, collectionEntry);
            }
        }
    }
}
Also used : RelationDescription(org.hibernate.envers.internal.entities.RelationDescription) AbstractCollectionPersister(org.hibernate.persister.collection.AbstractCollectionPersister)

Example 3 with AbstractCollectionPersister

use of org.hibernate.persister.collection.AbstractCollectionPersister in project hibernate-orm by hibernate.

the class BaseEnversCollectionEventListener method onCollectionAction.

protected final void onCollectionAction(AbstractCollectionEvent event, PersistentCollection newColl, Serializable oldColl, CollectionEntry collectionEntry) {
    if (shouldGenerateRevision(event)) {
        checkIfTransactionInProgress(event.getSession());
        final AuditProcess auditProcess = getEnversService().getAuditProcessManager().get(event.getSession());
        final String entityName = event.getAffectedOwnerEntityName();
        final String ownerEntityName = ((AbstractCollectionPersister) collectionEntry.getLoadedPersister()).getOwnerEntityName();
        final String referencingPropertyName = collectionEntry.getRole().substring(ownerEntityName.length() + 1);
        // Checking if this is not a "fake" many-to-one bidirectional relation. The relation description may be
        // null in case of collections of non-entities.
        final RelationDescription rd = searchForRelationDescription(entityName, referencingPropertyName);
        if (rd != null && rd.getMappedByPropertyName() != null) {
            generateFakeBidirecationalRelationWorkUnits(auditProcess, newColl, oldColl, entityName, referencingPropertyName, event, rd);
        } else {
            final PersistentCollectionChangeWorkUnit workUnit = new PersistentCollectionChangeWorkUnit(event.getSession(), entityName, getEnversService(), newColl, collectionEntry, oldColl, event.getAffectedOwnerIdOrNull(), referencingPropertyName);
            auditProcess.addWorkUnit(workUnit);
            if (workUnit.containsWork()) {
                // There are some changes: a revision needs also be generated for the collection owner
                auditProcess.addWorkUnit(new CollectionChangeWorkUnit(event.getSession(), event.getAffectedOwnerEntityName(), referencingPropertyName, getEnversService(), event.getAffectedOwnerIdOrNull(), event.getAffectedOwnerOrNull()));
                generateBidirectionalCollectionChangeWorkUnits(auditProcess, event, workUnit, rd);
            }
        }
    }
}
Also used : AuditProcess(org.hibernate.envers.internal.synchronization.AuditProcess) RelationDescription(org.hibernate.envers.internal.entities.RelationDescription) PersistentCollectionChangeWorkUnit(org.hibernate.envers.internal.synchronization.work.PersistentCollectionChangeWorkUnit) AbstractCollectionPersister(org.hibernate.persister.collection.AbstractCollectionPersister) CollectionChangeWorkUnit(org.hibernate.envers.internal.synchronization.work.CollectionChangeWorkUnit) PersistentCollectionChangeWorkUnit(org.hibernate.envers.internal.synchronization.work.PersistentCollectionChangeWorkUnit)

Aggregations

AbstractCollectionPersister (org.hibernate.persister.collection.AbstractCollectionPersister)3 RelationDescription (org.hibernate.envers.internal.entities.RelationDescription)2 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 AuditProcess (org.hibernate.envers.internal.synchronization.AuditProcess)1 CollectionChangeWorkUnit (org.hibernate.envers.internal.synchronization.work.CollectionChangeWorkUnit)1 PersistentCollectionChangeWorkUnit (org.hibernate.envers.internal.synchronization.work.PersistentCollectionChangeWorkUnit)1 CollectionType (org.hibernate.type.CollectionType)1 Type (org.hibernate.type.Type)1