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