Search in sources :

Example 1 with EntityConfiguration

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

the class AbstractToOneMapper method getEntityInfo.

/**
	 * @param enversService The EnversService
	 * @param entityName Entity name.
	 *
	 * @return Entity class, name and information whether it is audited or not.
	 */
protected EntityInfo getEntityInfo(EnversService enversService, String entityName) {
    EntityConfiguration entCfg = enversService.getEntitiesConfigurations().get(entityName);
    boolean isRelationAudited = true;
    if (entCfg == null) {
        // a relation marked as RelationTargetAuditMode.NOT_AUDITED
        entCfg = enversService.getEntitiesConfigurations().getNotVersionEntityConfiguration(entityName);
        isRelationAudited = false;
    }
    final Class entityClass = ReflectionTools.loadClass(entCfg.getEntityClassName(), enversService.getClassLoaderService());
    return new EntityInfo(entityClass, entityName, isRelationAudited);
}
Also used : EntityConfiguration(org.hibernate.envers.internal.entities.EntityConfiguration)

Example 2 with EntityConfiguration

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

the class BaseEnversCollectionEventListener method searchForRelationDescription.

/**
	 * Looks up a relation description corresponding to the given property in the given entity. If no description is
	 * found in the given entity, the parent entity is checked (so that inherited relations work).
	 *
	 * @param entityName Name of the entity, in which to start looking.
	 * @param referencingPropertyName The name of the property.
	 *
	 * @return A found relation description corresponding to the given entity or {@code null}, if no description can
	 *         be found.
	 */
private RelationDescription searchForRelationDescription(String entityName, String referencingPropertyName) {
    final EntityConfiguration configuration = getEnversService().getEntitiesConfigurations().get(entityName);
    final String propertyName = sanitizeReferencingPropertyName(referencingPropertyName);
    final RelationDescription rd = configuration.getRelationDescription(propertyName);
    if (rd == null && configuration.getParentEntityName() != null) {
        return searchForRelationDescription(configuration.getParentEntityName(), propertyName);
    }
    return rd;
}
Also used : EntityConfiguration(org.hibernate.envers.internal.entities.EntityConfiguration) RelationDescription(org.hibernate.envers.internal.entities.RelationDescription)

Example 3 with EntityConfiguration

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

the class AuditMetadataGenerator method generateFirstPass.

@SuppressWarnings({ "unchecked" })
public void generateFirstPass(PersistentClass pc, ClassAuditingData auditingData, EntityXmlMappingData xmlMappingData, boolean isAudited) {
    final String schema = getSchema(auditingData.getAuditTable().schema(), pc.getTable());
    final String catalog = getCatalog(auditingData.getAuditTable().catalog(), pc.getTable());
    if (!isAudited) {
        final String entityName = pc.getEntityName();
        final IdMappingData idMapper = idMetadataGenerator.addId(pc, false);
        if (idMapper == null) {
            // Unsupported id mapping, e.g. key-many-to-one. If the entity is used in auditing, an exception
            // will be thrown later on.
            LOG.debugf("Unable to create auditing id mapping for entity %s, because of an unsupported Hibernate id mapping (e.g. key-many-to-one)", entityName);
            return;
        }
        final ExtendedPropertyMapper propertyMapper = null;
        final String parentEntityName = null;
        final EntityConfiguration entityCfg = new EntityConfiguration(entityName, pc.getClassName(), idMapper, propertyMapper, parentEntityName);
        notAuditedEntitiesConfigurations.put(entityName, entityCfg);
        return;
    }
    final String entityName = pc.getEntityName();
    LOG.debugf("Generating first-pass auditing mapping for entity %s", entityName);
    final String auditEntityName = verEntCfg.getAuditEntityName(entityName);
    final String auditTableName = verEntCfg.getAuditTableName(entityName, pc.getTable().getName());
    // Registering the audit entity name, now that it is known
    auditEntityNameRegister.register(auditEntityName);
    final AuditTableData auditTableData = new AuditTableData(auditEntityName, auditTableName, schema, catalog);
    // Generating a mapping for the id
    final IdMappingData idMapper = idMetadataGenerator.addId(pc, true);
    final InheritanceType inheritanceType = InheritanceType.get(pc);
    // These properties will be read from the mapping data
    final Element classMapping;
    final ExtendedPropertyMapper propertyMapper;
    final String parentEntityName;
    final Triple<Element, ExtendedPropertyMapper, String> mappingData;
    // Reading the mapping data depending on inheritance type (if any)
    switch(inheritanceType) {
        case NONE:
            mappingData = generateMappingData(pc, xmlMappingData, auditTableData, idMapper);
            break;
        case SINGLE:
            mappingData = generateInheritanceMappingData(pc, xmlMappingData, auditTableData, "subclass");
            break;
        case JOINED:
            mappingData = generateInheritanceMappingData(pc, xmlMappingData, auditTableData, "joined-subclass");
            // Adding the "key" element with all id columns...
            final Element keyMapping = mappingData.getFirst().addElement("key");
            MetadataTools.addColumns(keyMapping, pc.getTable().getPrimaryKey().columnIterator());
            // ... and the revision number column, read from the revision info relation mapping.
            keyMapping.add((Element) cloneAndSetupRevisionInfoRelationMapping().element("column").clone());
            break;
        case TABLE_PER_CLASS:
            mappingData = generateInheritanceMappingData(pc, xmlMappingData, auditTableData, "union-subclass");
            break;
        default:
            throw new AssertionError("Impossible enum value.");
    }
    classMapping = mappingData.getFirst();
    propertyMapper = mappingData.getSecond();
    parentEntityName = mappingData.getThird();
    xmlMappingData.setClassMapping(classMapping);
    // Mapping unjoined properties
    addProperties(classMapping, pc.getUnjoinedPropertyIterator(), propertyMapper, auditingData, pc.getEntityName(), xmlMappingData, true);
    // Creating and mapping joins (first pass)
    createJoins(pc, classMapping, auditingData);
    addJoins(pc, propertyMapper, auditingData, pc.getEntityName(), xmlMappingData, true);
    // HHH-7940 - New synthetic property support for @IndexColumn/@OrderColumn dynamic properties
    addSynthetics(classMapping, auditingData, propertyMapper, xmlMappingData, pc.getEntityName(), true);
    // Storing the generated configuration
    final EntityConfiguration entityCfg = new EntityConfiguration(auditEntityName, pc.getClassName(), idMapper, propertyMapper, parentEntityName);
    entitiesConfigurations.put(pc.getEntityName(), entityCfg);
}
Also used : Element(org.dom4j.Element) EntityConfiguration(org.hibernate.envers.internal.entities.EntityConfiguration) ExtendedPropertyMapper(org.hibernate.envers.internal.entities.mapper.ExtendedPropertyMapper) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Example 4 with EntityConfiguration

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

the class AuditMetadataGenerator method getReferencedIdMappingData.

/**
	 * Reads the id mapping data of a referenced entity.
	 *
	 * @param entityName Name of the entity which is the source of the relation.
	 * @param referencedEntityName Name of the entity which is the target of the relation.
	 * @param propertyAuditingData Auditing data of the property that is the source of the relation.
	 * @param allowNotAuditedTarget Are not-audited target entities allowed.
	 *
	 * @return The id mapping data of the related entity.
	 *
	 * @throws MappingException If a relation from an audited to a non-audited entity is detected, which is not
	 * mapped using {@link RelationTargetAuditMode#NOT_AUDITED}.
	 */
IdMappingData getReferencedIdMappingData(String entityName, String referencedEntityName, PropertyAuditingData propertyAuditingData, boolean allowNotAuditedTarget) {
    EntityConfiguration configuration = getEntitiesConfigurations().get(referencedEntityName);
    if (configuration == null) {
        final RelationTargetAuditMode relationTargetAuditMode = propertyAuditingData.getRelationTargetAuditMode();
        configuration = getNotAuditedEntitiesConfigurations().get(referencedEntityName);
        if (configuration == null || !allowNotAuditedTarget || !RelationTargetAuditMode.NOT_AUDITED.equals(relationTargetAuditMode)) {
            throw new MappingException("An audited relation from " + entityName + "." + propertyAuditingData.getName() + " to a not audited entity " + referencedEntityName + "!" + (allowNotAuditedTarget ? " Such mapping is possible, but has to be explicitly defined using @Audited(targetAuditMode = NOT_AUDITED)." : ""));
        }
    }
    return configuration.getIdMappingData();
}
Also used : RelationTargetAuditMode(org.hibernate.envers.RelationTargetAuditMode) EntityConfiguration(org.hibernate.envers.internal.entities.EntityConfiguration) MappingException(org.hibernate.MappingException)

Example 5 with EntityConfiguration

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

the class AuditMetadataGenerator method generateInheritanceMappingData.

private Triple<Element, ExtendedPropertyMapper, String> generateInheritanceMappingData(PersistentClass pc, EntityXmlMappingData xmlMappingData, AuditTableData auditTableData, String inheritanceMappingType) {
    final String extendsEntityName = verEntCfg.getAuditEntityName(pc.getSuperclass().getEntityName());
    final Element classMapping = MetadataTools.createSubclassEntity(xmlMappingData.getMainXmlMapping(), inheritanceMappingType, auditTableData, extendsEntityName, pc.getDiscriminatorValue(), pc.isAbstract());
    // The id and revision type is already mapped in the parent
    // Getting the property mapper of the parent - when mapping properties, they need to be included
    final String parentEntityName = pc.getSuperclass().getEntityName();
    final EntityConfiguration parentConfiguration = entitiesConfigurations.get(parentEntityName);
    if (parentConfiguration == null) {
        throw new MappingException("Entity '" + pc.getEntityName() + "' is audited, but its superclass: '" + parentEntityName + "' is not.");
    }
    final ExtendedPropertyMapper parentPropertyMapper = parentConfiguration.getPropertyMapper();
    final ExtendedPropertyMapper propertyMapper = new SubclassPropertyMapper(new MultiPropertyMapper(), parentPropertyMapper);
    return Triple.make(classMapping, propertyMapper, parentEntityName);
}
Also used : Element(org.dom4j.Element) EntityConfiguration(org.hibernate.envers.internal.entities.EntityConfiguration) SubclassPropertyMapper(org.hibernate.envers.internal.entities.mapper.SubclassPropertyMapper) MultiPropertyMapper(org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper) ExtendedPropertyMapper(org.hibernate.envers.internal.entities.mapper.ExtendedPropertyMapper) MappingException(org.hibernate.MappingException)

Aggregations

EntityConfiguration (org.hibernate.envers.internal.entities.EntityConfiguration)7 MappingException (org.hibernate.MappingException)3 Element (org.dom4j.Element)2 IdMappingData (org.hibernate.envers.internal.entities.IdMappingData)2 ExtendedPropertyMapper (org.hibernate.envers.internal.entities.mapper.ExtendedPropertyMapper)2 HashMap (java.util.HashMap)1 RelationTargetAuditMode (org.hibernate.envers.RelationTargetAuditMode)1 PropertyData (org.hibernate.envers.internal.entities.PropertyData)1 RelationDescription (org.hibernate.envers.internal.entities.RelationDescription)1 MultiPropertyMapper (org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper)1 PropertyMapper (org.hibernate.envers.internal.entities.mapper.PropertyMapper)1 SubclassPropertyMapper (org.hibernate.envers.internal.entities.mapper.SubclassPropertyMapper)1 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)1 OneToOneNotOwningMapper (org.hibernate.envers.internal.entities.mapper.relation.OneToOneNotOwningMapper)1 ToOneIdMapper (org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper)1 OneToOne (org.hibernate.mapping.OneToOne)1