Search in sources :

Example 1 with MiddleRelatedComponentMapper

use of org.hibernate.envers.internal.entities.mapper.relation.component.MiddleRelatedComponentMapper in project hibernate-orm by hibernate.

the class CollectionMetadataGenerator method addValueToMiddleTable.

/**
 * @param value Value, which should be mapped to the middle-table, either as a relation to another entity,
 * or as a simple value.
 * @param xmlMapping If not <code>null</code>, xml mapping for this value is added to this element.
 * @param queryGeneratorBuilder In case <code>value</code> is a relation to another entity, information about it
 * should be added to the given.
 * @param prefix Prefix for proeprty names of related entities identifiers.
 * @param joinColumns Names of columns to use in the xml mapping, if this array isn't null and has any elements.
 *
 * @return Data for mapping this component.
 */
@SuppressWarnings({ "unchecked" })
private MiddleComponentData addValueToMiddleTable(Value value, Element xmlMapping, QueryGeneratorBuilder queryGeneratorBuilder, String prefix, JoinColumn[] joinColumns, boolean key) {
    final Type type = value.getType();
    if (type instanceof ManyToOneType) {
        final String prefixRelated = prefix + "_";
        final String referencedEntityName = MappingTools.getReferencedEntityName(value);
        final IdMappingData referencedIdMapping = mainGenerator.getReferencedIdMappingData(referencingEntityName, referencedEntityName, propertyAuditingData, true);
        // relation isn't inverse (so when <code>xmlMapping</code> is not null).
        if (xmlMapping != null) {
            addRelatedToXmlMapping(xmlMapping, prefixRelated, joinColumns != null && joinColumns.length > 0 ? MetadataTools.getColumnNameIterator(joinColumns) : MetadataTools.getColumnNameIterator(value.getColumnIterator()), referencedIdMapping);
        }
        // Storing the id data of the referenced entity: original mapper, prefixed mapper and entity name.
        final MiddleIdData referencedIdData = createMiddleIdData(referencedIdMapping, prefixRelated, referencedEntityName);
        // And adding it to the generator builder.
        queryGeneratorBuilder.addRelation(referencedIdData);
        return new MiddleComponentData(new MiddleRelatedComponentMapper(referencedIdData), queryGeneratorBuilder.getCurrentIndex());
    } else if (type instanceof ComponentType) {
        // Collection of embeddable elements.
        final Component component = (Component) value;
        final Class componentClass = ReflectionTools.loadClass(component.getComponentClassName(), mainGenerator.getClassLoaderService());
        final MiddleEmbeddableComponentMapper componentMapper = new MiddleEmbeddableComponentMapper(new MultiPropertyMapper(), componentClass);
        final Element parentXmlMapping = xmlMapping.getParent();
        final ComponentAuditingData auditData = new ComponentAuditingData();
        final ReflectionManager reflectionManager = mainGenerator.getMetadata().getMetadataBuildingOptions().getReflectionManager();
        new ComponentAuditedPropertiesReader(ModificationStore.FULL, new AuditedPropertiesReader.ComponentPropertiesSource(reflectionManager, component), auditData, mainGenerator.getGlobalCfg(), reflectionManager, "").read();
        // Emulating first pass.
        for (String auditedPropertyName : auditData.getPropertyNames()) {
            final PropertyAuditingData nestedAuditingData = auditData.getPropertyAuditingData(auditedPropertyName);
            mainGenerator.addValue(parentXmlMapping, component.getProperty(auditedPropertyName).getValue(), componentMapper, prefix, xmlMappingData, nestedAuditingData, true, true, true);
        }
        // Emulating second pass so that the relations can be mapped too.
        for (String auditedPropertyName : auditData.getPropertyNames()) {
            final PropertyAuditingData nestedAuditingData = auditData.getPropertyAuditingData(auditedPropertyName);
            mainGenerator.addValue(parentXmlMapping, component.getProperty(auditedPropertyName).getValue(), componentMapper, referencingEntityName, xmlMappingData, nestedAuditingData, true, false, true);
        }
        // Embeddable properties may contain null values, so cannot be stored within composite primary key.
        if (propertyValue.isSet()) {
            final String setOrdinalPropertyName = mainGenerator.getVerEntCfg().getEmbeddableSetOrdinalPropertyName();
            final Element ordinalProperty = MetadataTools.addProperty(xmlMapping, setOrdinalPropertyName, "integer", true, true);
            MetadataTools.addColumn(ordinalProperty, setOrdinalPropertyName, null, null, null, null, null, null, false);
        }
        return new MiddleComponentData(componentMapper, 0);
    } else {
        // Last but one parameter: collection components are always insertable
        final boolean mapped = mainGenerator.getBasicMetadataGenerator().addBasic(key ? xmlMapping : xmlMapping.getParent(), new PropertyAuditingData(prefix, "field", ModificationStore.FULL, RelationTargetAuditMode.AUDITED, null, null, false), value, null, true, key);
        if (mapped && key) {
            // Simple values are always stored in the first item of the array returned by the query generator.
            return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
        } else if (mapped && !key) {
            // when mapped but not part of the key, its stored as a dummy mapper??
            return new MiddleComponentData(new MiddleMapElementNotKeyComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0);
        } else {
            mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName);
            // Impossible to get here.
            throw new AssertionError();
        }
    }
}
Also used : AuditedPropertiesReader(org.hibernate.envers.configuration.internal.metadata.reader.AuditedPropertiesReader) ComponentAuditedPropertiesReader(org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditedPropertiesReader) ComponentType(org.hibernate.type.ComponentType) ManyToOneType(org.hibernate.type.ManyToOneType) ReflectionManager(org.hibernate.annotations.common.reflection.ReflectionManager) MiddleSimpleComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleSimpleComponentMapper) MiddleRelatedComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleRelatedComponentMapper) Element(org.dom4j.Element) MultiPropertyMapper(org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper) PropertyAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData) MiddleMapElementNotKeyComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleMapElementNotKeyComponentMapper) 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) ComponentAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditingData) MiddleIdData(org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData) ComponentAuditedPropertiesReader(org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditedPropertiesReader) MiddleEmbeddableComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleEmbeddableComponentMapper) PersistentClass(org.hibernate.mapping.PersistentClass) MiddleComponentData(org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData) Component(org.hibernate.mapping.Component) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Example 2 with MiddleRelatedComponentMapper

use of org.hibernate.envers.internal.entities.mapper.relation.component.MiddleRelatedComponentMapper 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)2 MultiPropertyMapper (org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper)2 MiddleComponentData (org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData)2 MiddleIdData (org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData)2 MiddleRelatedComponentMapper (org.hibernate.envers.internal.entities.mapper.relation.component.MiddleRelatedComponentMapper)2 BagType (org.hibernate.type.BagType)2 ComponentType (org.hibernate.type.ComponentType)2 ListType (org.hibernate.type.ListType)2 ManyToOneType (org.hibernate.type.ManyToOneType)2 MapType (org.hibernate.type.MapType)2 MaterializedClobType (org.hibernate.type.MaterializedClobType)2 MaterializedNClobType (org.hibernate.type.MaterializedNClobType)2 SetType (org.hibernate.type.SetType)2 SortedMapType (org.hibernate.type.SortedMapType)2 SortedSetType (org.hibernate.type.SortedSetType)2 Type (org.hibernate.type.Type)2 Element (org.dom4j.Element)1 ReflectionManager (org.hibernate.annotations.common.reflection.ReflectionManager)1 AuditedPropertiesReader (org.hibernate.envers.configuration.internal.metadata.reader.AuditedPropertiesReader)1 ComponentAuditedPropertiesReader (org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditedPropertiesReader)1