Search in sources :

Example 6 with IdMapper

use of org.hibernate.envers.internal.entities.mapper.id.IdMapper in project hibernate-orm by hibernate.

the class BaseEnversEventListener method addCollectionChangeWorkUnit.

private void addCollectionChangeWorkUnit(AuditProcess auditProcess, SessionImplementor session, String fromEntityName, RelationDescription relDesc, Object value) {
    // relDesc.getToEntityName() doesn't always return the entity name of the value - in case
    // of subclasses, this will be root class, no the actual class. So it can't be used here.
    String toEntityName;
    Serializable id;
    if (value instanceof HibernateProxy) {
        final HibernateProxy hibernateProxy = (HibernateProxy) value;
        id = hibernateProxy.getHibernateLazyInitializer().getIdentifier();
        // We've got to initialize the object from the proxy to later read its state.
        value = EntityTools.getTargetFromProxy(session.getFactory(), hibernateProxy);
        // HHH-7249
        // This call must occur after the proxy has been initialized or the returned name will
        // be to the base class which will impact the discriminator value chosen when using an
        // inheritance strategy with discriminators.
        toEntityName = session.bestGuessEntityName(value);
    } else {
        toEntityName = session.guessEntityName(value);
        final IdMapper idMapper = enversService.getEntitiesConfigurations().get(toEntityName).getIdMapper();
        id = (Serializable) idMapper.mapToIdFromEntity(value);
    }
    final Set<String> toPropertyNames = enversService.getEntitiesConfigurations().getToPropertyNames(fromEntityName, relDesc.getFromPropertyName(), toEntityName);
    final String toPropertyName = toPropertyNames.iterator().next();
    auditProcess.addWorkUnit(new CollectionChangeWorkUnit(session, toEntityName, toPropertyName, enversService, id, value));
}
Also used : Serializable(java.io.Serializable) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) HibernateProxy(org.hibernate.proxy.HibernateProxy) CollectionChangeWorkUnit(org.hibernate.envers.internal.synchronization.work.CollectionChangeWorkUnit)

Example 7 with IdMapper

use of org.hibernate.envers.internal.entities.mapper.id.IdMapper in project hibernate-orm by hibernate.

the class EntityInstantiator method createInstanceFromVersionsEntity.

/**
	 * Creates an entity instance based on an entry from the versions table.
	 *
	 * @param entityName Name of the entity, which instances should be read
	 * @param versionsEntity An entry in the versions table, from which data should be mapped.
	 * @param revision Revision at which this entity was read.
	 *
	 * @return An entity instance, with versioned properties set as in the versionsEntity map, and proxies
	 *         created for collections.
	 */
public Object createInstanceFromVersionsEntity(String entityName, Map versionsEntity, Number revision) {
    if (versionsEntity == null) {
        return null;
    }
    // The $type$ property holds the name of the (versions) entity
    final String type = enversService.getEntitiesConfigurations().getEntityNameForVersionsEntityName((String) versionsEntity.get("$type$"));
    if (type != null) {
        entityName = type;
    }
    // First mapping the primary key
    final IdMapper idMapper = enversService.getEntitiesConfigurations().get(entityName).getIdMapper();
    final Map originalId = (Map) versionsEntity.get(enversService.getAuditEntitiesConfiguration().getOriginalIdPropName());
    // Fixes HHH-4751 issue (@IdClass with @ManyToOne relation mapping inside)
    // Note that identifiers are always audited
    // Replace identifier proxies if do not point to audit tables
    replaceNonAuditIdProxies(versionsEntity, revision);
    final Object primaryKey = idMapper.mapToIdFromMap(originalId);
    // Checking if the entity is in cache
    if (versionsReader.getFirstLevelCache().contains(entityName, revision, primaryKey)) {
        return versionsReader.getFirstLevelCache().get(entityName, revision, primaryKey);
    }
    // If it is not in the cache, creating a new entity instance
    Object ret;
    try {
        EntityConfiguration entCfg = enversService.getEntitiesConfigurations().get(entityName);
        if (entCfg == null) {
            // a relation marked as RelationTargetAuditMode.NOT_AUDITED
            entCfg = enversService.getEntitiesConfigurations().getNotVersionEntityConfiguration(entityName);
        }
        final Class<?> cls = ReflectionTools.loadClass(entCfg.getEntityClassName(), enversService.getClassLoaderService());
        ret = ReflectHelper.getDefaultConstructor(cls).newInstance();
    } catch (Exception e) {
        throw new AuditException(e);
    }
    // Putting the newly created entity instance into the first level cache, in case a one-to-one bidirectional
    // relation is present (which is eagerly loaded).
    versionsReader.getFirstLevelCache().put(entityName, revision, primaryKey, ret);
    enversService.getEntitiesConfigurations().get(entityName).getPropertyMapper().mapToEntityFromMap(enversService, ret, versionsEntity, primaryKey, versionsReader, revision);
    idMapper.mapToEntityFromMap(ret, originalId);
    // Put entity on entityName cache afterQuery mapping it from the map representation
    versionsReader.getFirstLevelCache().putOnEntityNameCache(primaryKey, revision, ret, entityName);
    return ret;
}
Also used : IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) AuditException(org.hibernate.envers.exception.AuditException) Map(java.util.Map) AuditException(org.hibernate.envers.exception.AuditException)

Example 8 with IdMapper

use of org.hibernate.envers.internal.entities.mapper.id.IdMapper in project hibernate-orm by hibernate.

the class ToOneRelationMetadataGenerator method addOneToOnePrimaryKeyJoinColumn.

@SuppressWarnings({ "unchecked" })
void addOneToOnePrimaryKeyJoinColumn(PropertyAuditingData propertyAuditingData, Value value, CompositeMapperBuilder mapper, String entityName, boolean insertable) {
    final String referencedEntityName = ((ToOne) value).getReferencedEntityName();
    final IdMappingData idMapping = mainGenerator.getReferencedIdMappingData(entityName, referencedEntityName, propertyAuditingData, true);
    final String lastPropertyPrefix = MappingTools.createToOneRelationPrefix(propertyAuditingData.getName());
    // Generating the id mapper for the relation
    final IdMapper relMapper = idMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
    // Storing information about this relation
    mainGenerator.getEntitiesConfigurations().get(entityName).addToOneRelation(propertyAuditingData.getName(), referencedEntityName, relMapper, insertable, MappingTools.ignoreNotFound(value));
    // Adding mapper for the id
    final PropertyData propertyData = propertyAuditingData.getPropertyData();
    mapper.addComposite(propertyData, new OneToOnePrimaryKeyJoinColumnMapper(entityName, referencedEntityName, propertyData, mainGenerator.getServiceRegistry()));
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData) OneToOnePrimaryKeyJoinColumnMapper(org.hibernate.envers.internal.entities.mapper.relation.OneToOnePrimaryKeyJoinColumnMapper) OneToOne(org.hibernate.mapping.OneToOne) ToOne(org.hibernate.mapping.ToOne) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Example 9 with IdMapper

use of org.hibernate.envers.internal.entities.mapper.id.IdMapper in project hibernate-orm by hibernate.

the class ToOneRelationMetadataGenerator method addOneToOneNotOwning.

@SuppressWarnings({ "unchecked" })
void addOneToOneNotOwning(PropertyAuditingData propertyAuditingData, Value value, CompositeMapperBuilder mapper, String entityName) {
    final OneToOne propertyValue = (OneToOne) value;
    final String owningReferencePropertyName = propertyValue.getReferencedPropertyName();
    final EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName);
    if (configuration == null) {
        throw new MappingException("An audited relation to a non-audited entity " + entityName + "!");
    }
    final IdMappingData ownedIdMapping = configuration.getIdMappingData();
    if (ownedIdMapping == null) {
        throw new MappingException("An audited relation to a non-audited entity " + entityName + "!");
    }
    final String lastPropertyPrefix = MappingTools.createToOneRelationPrefix(owningReferencePropertyName);
    final String referencedEntityName = propertyValue.getReferencedEntityName();
    // Generating the id mapper for the relation
    final IdMapper ownedIdMapper = ownedIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
    // Storing information about this relation
    mainGenerator.getEntitiesConfigurations().get(entityName).addToOneNotOwningRelation(propertyAuditingData.getName(), owningReferencePropertyName, referencedEntityName, ownedIdMapper, MappingTools.ignoreNotFound(value));
    // Adding mapper for the id
    final PropertyData propertyData = propertyAuditingData.getPropertyData();
    mapper.addComposite(propertyData, new OneToOneNotOwningMapper(entityName, referencedEntityName, owningReferencePropertyName, propertyData, mainGenerator.getServiceRegistry()));
}
Also used : OneToOne(org.hibernate.mapping.OneToOne) PropertyData(org.hibernate.envers.internal.entities.PropertyData) OneToOneNotOwningMapper(org.hibernate.envers.internal.entities.mapper.relation.OneToOneNotOwningMapper) EntityConfiguration(org.hibernate.envers.internal.entities.EntityConfiguration) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) MappingException(org.hibernate.MappingException) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Example 10 with IdMapper

use of org.hibernate.envers.internal.entities.mapper.id.IdMapper in project hibernate-orm by hibernate.

the class AuditAssociationQueryImpl method addCriterionsToQuery.

protected void addCriterionsToQuery(AuditReaderImplementor versionsReader) {
    if (enversService.getEntitiesConfigurations().isVersioned(entityName)) {
        String auditEntityName = enversService.getAuditEntitiesConfiguration().getAuditEntityName(entityName);
        Parameters joinConditionParameters = queryBuilder.addJoin(joinType, auditEntityName, alias, false);
        // owner.reference_id = target.originalId.id
        AuditEntitiesConfiguration verEntCfg = enversService.getAuditEntitiesConfiguration();
        String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
        IdMapper idMapperTarget = enversService.getEntitiesConfigurations().get(entityName).getIdMapper();
        final String prefix = alias.concat(".").concat(originalIdPropertyName);
        ownerAssociationIdMapper.addIdsEqualToQuery(joinConditionParameters, ownerAlias, idMapperTarget, prefix);
        // filter revision of target entity
        Parameters parametersToUse = parameters;
        String revisionPropertyPath = verEntCfg.getRevisionNumberPath();
        if (joinType == JoinType.LEFT) {
            parametersToUse = parameters.addSubParameters(Parameters.OR);
            parametersToUse.addNullRestriction(revisionPropertyPath, true);
            parametersToUse = parametersToUse.addSubParameters(Parameters.AND);
        }
        MiddleIdData referencedIdData = new MiddleIdData(verEntCfg, enversService.getEntitiesConfigurations().get(entityName).getIdMappingData(), null, entityName, enversService.getEntitiesConfigurations().isVersioned(entityName));
        enversService.getAuditStrategy().addEntityAtRevisionRestriction(enversService.getGlobalConfiguration(), queryBuilder, parametersToUse, revisionPropertyPath, verEntCfg.getRevisionEndFieldName(), true, referencedIdData, revisionPropertyPath, originalIdPropertyName, alias, queryBuilder.generateAlias(), true);
    } else {
        Parameters joinConditionParameters = queryBuilder.addJoin(joinType, entityName, alias, false);
        // owner.reference_id = target.id
        final IdMapper idMapperTarget = enversService.getEntitiesConfigurations().getNotVersionEntityConfiguration(entityName).getIdMapper();
        ownerAssociationIdMapper.addIdsEqualToQuery(joinConditionParameters, ownerAlias, idMapperTarget, alias);
    }
    for (AuditCriterion criterion : criterions) {
        criterion.addToQuery(enversService, versionsReader, aliasToEntityNameMap, alias, queryBuilder, parameters);
    }
    for (final AuditAssociationQueryImpl<?> sub : associationQueries) {
        sub.addCriterionsToQuery(versionsReader);
    }
}
Also used : Parameters(org.hibernate.envers.internal.tools.query.Parameters) MiddleIdData(org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData) AuditEntitiesConfiguration(org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) AuditCriterion(org.hibernate.envers.query.criteria.AuditCriterion)

Aggregations

IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)10 IdMappingData (org.hibernate.envers.internal.entities.IdMappingData)4 PropertyData (org.hibernate.envers.internal.entities.PropertyData)4 ToOneIdMapper (org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper)4 Serializable (java.io.Serializable)3 CollectionChangeWorkUnit (org.hibernate.envers.internal.synchronization.work.CollectionChangeWorkUnit)3 OneToOne (org.hibernate.mapping.OneToOne)3 PersistentCollectionChangeData (org.hibernate.envers.internal.entities.mapper.PersistentCollectionChangeData)2 MiddleIdData (org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData)2 PersistentCollectionChangeWorkUnit (org.hibernate.envers.internal.synchronization.work.PersistentCollectionChangeWorkUnit)2 ToOne (org.hibernate.mapping.ToOne)2 Map (java.util.Map)1 Element (org.dom4j.Element)1 MappingException (org.hibernate.MappingException)1 RevisionType (org.hibernate.envers.RevisionType)1 AuditEntitiesConfiguration (org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration)1 AuditException (org.hibernate.envers.exception.AuditException)1 EntityConfiguration (org.hibernate.envers.internal.entities.EntityConfiguration)1 MultiPropertyMapper (org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper)1 PropertyMapper (org.hibernate.envers.internal.entities.mapper.PropertyMapper)1