if ( type != null ) {
entityName = type;
}
// First mapping the primary key
final IdMapper idMapper = verCfg.getEntCfg().get( entityName ).getIdMapper();
final Map originalId = (Map) versionsEntity.get( verCfg.getAuditEntCfg().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 = verCfg.getEntCfg().get( entityName );
if ( entCfg == null ) {
// a relation marked as RelationTargetAuditMode.NOT_AUDITED
entCfg = verCfg.getEntCfg().getNotVersionEntityConfiguration( entityName );
}
final Class<?> cls = ReflectionTools.loadClass( entCfg.getEntityClassName(), verCfg.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 );
verCfg.getEntCfg().get( entityName ).getPropertyMapper().mapToEntityFromMap(
verCfg,
ret,
versionsEntity,
primaryKey,
versionsReader,
revision
);
idMapper.mapToEntityFromMap( ret, originalId );
// Put entity on entityName cache after mapping it from the map representation
versionsReader.getFirstLevelCache().putOnEntityNameCache( primaryKey, revision, ret, entityName );
return ret;