Search in sources :

Example 6 with IdMappingData

use of org.hibernate.envers.internal.entities.IdMappingData 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 7 with IdMappingData

use of org.hibernate.envers.internal.entities.IdMappingData 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 8 with IdMappingData

use of org.hibernate.envers.internal.entities.IdMappingData 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 9 with IdMappingData

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

the class CollectionMetadataGenerator method addOneToManyAttached.

@SuppressWarnings({ "unchecked" })
private void addOneToManyAttached(boolean fakeOneToManyBidirectional) {
    LOG.debugf("Adding audit mapping for property %s.%s: one-to-many collection, using a join column on the referenced entity", referencingEntityName, propertyName);
    // check whether the property has an @IndexColumn or @OrderColumn because its part of an
    // IndexedCollection mapping type.
    final boolean indexed = (propertyValue instanceof IndexedCollection) && ((IndexedCollection) propertyValue).getIndex() != null;
    final String mappedBy = getMappedBy(propertyValue);
    final IdMappingData referencedIdMapping = mainGenerator.getReferencedIdMappingData(referencingEntityName, referencedEntityName, propertyAuditingData, false);
    final IdMappingData referencingIdMapping = referencingEntityConfiguration.getIdMappingData();
    // Generating the id mappers data for the referencing side of the relation.
    final MiddleIdData referencingIdData = createMiddleIdData(referencingIdMapping, mappedBy + "_", referencingEntityName);
    // And for the referenced side. The prefixed mapper won't be used (as this collection isn't persisted
    // in a join table, so the prefix value is arbitrary).
    final MiddleIdData referencedIdData = createMiddleIdData(referencedIdMapping, null, referencedEntityName);
    // Generating the element mapping.
    final MiddleComponentData elementComponentData = new MiddleComponentData(new MiddleRelatedComponentMapper(referencedIdData), 0);
    // Generating the index mapping, if an index exists. It can only exists in case a javax.persistence.MapKey
    // annotation is present on the entity. So the middleEntityXml will be not be used. The queryGeneratorBuilder
    // will only be checked for nullnes.
    MiddleComponentData indexComponentData = addIndex(null, null);
    // Generating the query generator - it should read directly from the related entity.
    final RelationQueryGenerator queryGenerator = new OneAuditEntityQueryGenerator(mainGenerator.getGlobalCfg(), mainGenerator.getVerEntCfg(), mainGenerator.getAuditStrategy(), referencingIdData, referencedEntityName, referencedIdData, isEmbeddableElementType(), mappedBy, isMappedByKey(propertyValue, mappedBy));
    // Creating common mapper data.
    final CommonCollectionMapperData commonCollectionMapperData = new CommonCollectionMapperData(mainGenerator.getVerEntCfg(), referencedEntityName, propertyAuditingData.getPropertyData(), referencingIdData, queryGenerator);
    PropertyMapper fakeBidirectionalRelationMapper;
    PropertyMapper fakeBidirectionalRelationIndexMapper;
    if (fakeOneToManyBidirectional || indexed) {
        // In case of a fake many-to-one bidirectional relation, we have to generate a mapper which maps
        // the mapped-by property name to the id of the related entity (which is the owner of the collection).
        final String auditMappedBy;
        if (fakeOneToManyBidirectional) {
            auditMappedBy = propertyAuditingData.getAuditMappedBy();
        } else {
            auditMappedBy = propertyValue.getMappedByProperty();
        }
        // Creating a prefixed relation mapper.
        final IdMapper relMapper = referencingIdMapping.getIdMapper().prefixMappedProperties(MappingTools.createToOneRelationPrefix(auditMappedBy));
        fakeBidirectionalRelationMapper = new ToOneIdMapper(relMapper, // when constructing the PropertyData.
        new PropertyData(auditMappedBy, null, null, null), referencingEntityName, false);
        final String positionMappedBy;
        if (fakeOneToManyBidirectional) {
            positionMappedBy = propertyAuditingData.getPositionMappedBy();
        } else if (indexed) {
            final Value indexValue = ((IndexedCollection) propertyValue).getIndex();
            positionMappedBy = indexValue.getColumnIterator().next().getText();
        } else {
            positionMappedBy = null;
        }
        // Checking if there's an index defined. If so, adding a mapper for it.
        if (positionMappedBy != null) {
            final Type indexType = ((IndexedCollection) propertyValue).getIndex().getType();
            fakeBidirectionalRelationIndexMapper = new SinglePropertyMapper(PropertyData.forProperty(positionMappedBy, indexType));
            // Also, overwriting the index component data to properly read the index.
            indexComponentData = new MiddleComponentData(new MiddleStraightComponentMapper(positionMappedBy), 0);
        } else {
            fakeBidirectionalRelationIndexMapper = null;
        }
    } else {
        fakeBidirectionalRelationMapper = null;
        fakeBidirectionalRelationIndexMapper = null;
    }
    // Checking the type of the collection and adding an appropriate mapper.
    addMapper(commonCollectionMapperData, elementComponentData, indexComponentData);
    // Storing information about this relation.
    referencingEntityConfiguration.addToManyNotOwningRelation(propertyName, mappedBy, referencedEntityName, referencingIdData.getPrefixedMapper(), fakeBidirectionalRelationMapper, fakeBidirectionalRelationIndexMapper, indexed);
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData) CommonCollectionMapperData(org.hibernate.envers.internal.entities.mapper.relation.CommonCollectionMapperData) MiddleRelatedComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleRelatedComponentMapper) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) SinglePropertyMapper(org.hibernate.envers.internal.entities.mapper.SinglePropertyMapper) MultiPropertyMapper(org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper) SinglePropertyMapper(org.hibernate.envers.internal.entities.mapper.SinglePropertyMapper) PropertyMapper(org.hibernate.envers.internal.entities.mapper.PropertyMapper) ListType(org.hibernate.type.ListType) ManyToOneType(org.hibernate.type.ManyToOneType) MaterializedNClobType(org.hibernate.type.MaterializedNClobType) SetType(org.hibernate.type.SetType) MaterializedClobType(org.hibernate.type.MaterializedClobType) MapType(org.hibernate.type.MapType) ComponentType(org.hibernate.type.ComponentType) SortedSetType(org.hibernate.type.SortedSetType) SortedMapType(org.hibernate.type.SortedMapType) BagType(org.hibernate.type.BagType) Type(org.hibernate.type.Type) MiddleIdData(org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData) RelationQueryGenerator(org.hibernate.envers.internal.entities.mapper.relation.query.RelationQueryGenerator) Value(org.hibernate.mapping.Value) MiddleComponentData(org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) OneAuditEntityQueryGenerator(org.hibernate.envers.internal.entities.mapper.relation.query.OneAuditEntityQueryGenerator) IndexedCollection(org.hibernate.mapping.IndexedCollection) MiddleStraightComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleStraightComponentMapper) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Aggregations

IdMappingData (org.hibernate.envers.internal.entities.IdMappingData)9 Element (org.dom4j.Element)5 PropertyData (org.hibernate.envers.internal.entities.PropertyData)4 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)4 MiddleComponentData (org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData)4 ToOneIdMapper (org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper)4 MiddleIdData (org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData)3 OneToOne (org.hibernate.mapping.OneToOne)3 EntityConfiguration (org.hibernate.envers.internal.entities.EntityConfiguration)2 MultiPropertyMapper (org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper)2 CommonCollectionMapperData (org.hibernate.envers.internal.entities.mapper.relation.CommonCollectionMapperData)2 MiddleRelatedComponentMapper (org.hibernate.envers.internal.entities.mapper.relation.component.MiddleRelatedComponentMapper)2 RelationQueryGenerator (org.hibernate.envers.internal.entities.mapper.relation.query.RelationQueryGenerator)2 Component (org.hibernate.mapping.Component)2 IndexedCollection (org.hibernate.mapping.IndexedCollection)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 ToOne (org.hibernate.mapping.ToOne)2 BagType (org.hibernate.type.BagType)2 ComponentType (org.hibernate.type.ComponentType)2 ListType (org.hibernate.type.ListType)2