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