Search in sources :

Example 6 with RelationDescription

use of org.hibernate.envers.internal.entities.RelationDescription in project hibernate-orm by hibernate.

the class NullAuditExpression method addToQuery.

@Override
protected void addToQuery(EnversService enversService, AuditReaderImplementor versionsReader, String entityName, String alias, QueryBuilder qb, Parameters parameters) {
    String propertyName = CriteriaTools.determinePropertyName(enversService, versionsReader, entityName, propertyNameGetter);
    RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(enversService, entityName, propertyName);
    if (relatedEntity == null) {
        parameters.addNullRestriction(alias, propertyName);
    } else {
        relatedEntity.getIdMapper().addIdEqualsToQuery(parameters, null, alias, null, true);
    }
}
Also used : RelationDescription(org.hibernate.envers.internal.entities.RelationDescription)

Example 7 with RelationDescription

use of org.hibernate.envers.internal.entities.RelationDescription in project hibernate-orm by hibernate.

the class BaseEnversEventListener method generateBidirectionalCollectionChangeWorkUnits.

protected final void generateBidirectionalCollectionChangeWorkUnits(AuditProcess auditProcess, EntityPersister entityPersister, String entityName, Object[] newState, Object[] oldState, SessionImplementor session) {
    // Checking if this is enabled in configuration ...
    if (!enversService.getGlobalConfiguration().isGenerateRevisionsForCollections()) {
        return;
    }
    // Checks every property of the entity, if it is an "owned" to-one relation to another entity.
    // If the value of that property changed, and the relation is bi-directional, a new revision
    // for the related entity is generated.
    final String[] propertyNames = entityPersister.getPropertyNames();
    for (int i = 0; i < propertyNames.length; i++) {
        final String propertyName = propertyNames[i];
        final RelationDescription relDesc = enversService.getEntitiesConfigurations().getRelationDescription(entityName, propertyName);
        if (relDesc != null && relDesc.isBidirectional() && relDesc.getRelationType() == RelationType.TO_ONE && relDesc.isInsertable()) {
            // Checking for changes
            final Object oldValue = oldState == null ? null : oldState[i];
            final Object newValue = newState == null ? null : newState[i];
            if (!EntityTools.entitiesEqual(session, relDesc.getToEntityName(), oldValue, newValue)) {
                // (size increases).
                if (newValue != null) {
                    addCollectionChangeWorkUnit(auditProcess, session, entityName, relDesc, newValue);
                }
                if (oldValue != null) {
                    addCollectionChangeWorkUnit(auditProcess, session, entityName, relDesc, oldValue);
                }
            }
        }
    }
}
Also used : RelationDescription(org.hibernate.envers.internal.entities.RelationDescription)

Example 8 with RelationDescription

use of org.hibernate.envers.internal.entities.RelationDescription 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 9 with RelationDescription

use of org.hibernate.envers.internal.entities.RelationDescription 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

RelationDescription (org.hibernate.envers.internal.entities.RelationDescription)9 AuditException (org.hibernate.envers.exception.AuditException)3 AbstractCollectionPersister (org.hibernate.persister.collection.AbstractCollectionPersister)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1 EntityConfiguration (org.hibernate.envers.internal.entities.EntityConfiguration)1 QueryParameterData (org.hibernate.envers.internal.entities.mapper.id.QueryParameterData)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 ComponentType (org.hibernate.type.ComponentType)1 Type (org.hibernate.type.Type)1