Search in sources :

Example 1 with IndexedCollection

use of org.hibernate.mapping.IndexedCollection in project hibernate-orm by hibernate.

the class CollectionMetadataGenerator method addIndex.

private MiddleComponentData addIndex(Element middleEntityXml, QueryGeneratorBuilder queryGeneratorBuilder) {
    if (propertyValue instanceof IndexedCollection) {
        final IndexedCollection indexedValue = (IndexedCollection) propertyValue;
        final String mapKey = propertyAuditingData.getMapKey();
        if (mapKey == null) {
            // This entity doesn't specify a javax.persistence.MapKey. Mapping it to the middle entity.
            return addValueToMiddleTable(indexedValue.getIndex(), middleEntityXml, queryGeneratorBuilder, "mapkey", null, true);
        } else {
            final IdMappingData referencedIdMapping = mainGenerator.getEntitiesConfigurations().get(referencedEntityName).getIdMappingData();
            final int currentIndex = queryGeneratorBuilder == null ? 0 : queryGeneratorBuilder.getCurrentIndex();
            if ("".equals(mapKey)) {
                // The key of the map is the id of the entity.
                return new MiddleComponentData(new MiddleMapKeyIdComponentMapper(mainGenerator.getVerEntCfg(), referencedIdMapping.getIdMapper()), currentIndex);
            } else {
                // The key of the map is a property of the entity.
                return new MiddleComponentData(new MiddleMapKeyPropertyComponentMapper(mapKey, propertyAuditingData.getAccessType()), currentIndex);
            }
        }
    } else {
        // No index - creating a dummy mapper.
        return new MiddleComponentData(new MiddleDummyComponentMapper(), 0);
    }
}
Also used : MiddleMapKeyPropertyComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleMapKeyPropertyComponentMapper) MiddleMapKeyIdComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleMapKeyIdComponentMapper) MiddleDummyComponentMapper(org.hibernate.envers.internal.entities.mapper.relation.component.MiddleDummyComponentMapper) MiddleComponentData(org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData) IndexedCollection(org.hibernate.mapping.IndexedCollection) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Example 2 with IndexedCollection

use of org.hibernate.mapping.IndexedCollection in project hibernate-orm by hibernate.

the class CollectionPropertyHolder method determineKeyClass.

private Class determineKeyClass(XClass keyXClass) {
    if (keyXClass != null) {
        try {
            return getContext().getBuildingOptions().getReflectionManager().toClass(keyXClass);
        } catch (Exception e) {
            log.debugf("Unable to resolve XClass [%s] to Class for collection key [%s]", keyXClass.getName(), collection.getRole());
        }
    }
    final IndexedCollection indexedCollection = (IndexedCollection) collection;
    if (indexedCollection.getIndex() != null) {
        if (indexedCollection.getIndex().getType() != null) {
            return indexedCollection.getIndex().getType().getReturnedClass();
        }
    }
    // currently this is called from paths where the element type really should be known,
    // so log the fact that we could not resolve the collection element info
    log.debugf("Unable to resolve key information for collection [%s]", collection.getRole());
    return null;
}
Also used : IndexedCollection(org.hibernate.mapping.IndexedCollection)

Example 3 with IndexedCollection

use of org.hibernate.mapping.IndexedCollection 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) {
            fakeBidirectionalRelationIndexMapper = new SinglePropertyMapper(new PropertyData(positionMappedBy, null, null, null));
            // 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) 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)

Example 4 with IndexedCollection

use of org.hibernate.mapping.IndexedCollection in project hibernate-orm by hibernate.

the class ElementCollectionTests method testSimpleConvertUsage.

@Test
public void testSimpleConvertUsage() throws MalformedURLException {
    // first some assertions of the metamodel
    PersistentClass entityBinding = metadata().getEntityBinding(TheEntity.class.getName());
    assertNotNull(entityBinding);
    Property setAttributeBinding = entityBinding.getProperty("set");
    Collection setBinding = (Collection) setAttributeBinding.getValue();
    assertTyping(AttributeConverterTypeAdapter.class, setBinding.getElement().getType());
    Property mapAttributeBinding = entityBinding.getProperty("map");
    IndexedCollection mapBinding = (IndexedCollection) mapAttributeBinding.getValue();
    assertTyping(AttributeConverterTypeAdapter.class, mapBinding.getIndex().getType());
    assertTyping(AttributeConverterTypeAdapter.class, mapBinding.getElement().getType());
    // now lets try to use the model, integration-testing-style!
    TheEntity entity = new TheEntity(1);
    Session s = openSession();
    s.beginTransaction();
    s.save(entity);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    TheEntity retrieved = (TheEntity) s.load(TheEntity.class, 1);
    assertEquals(1, retrieved.getSet().size());
    assertEquals(new ValueType("set_value"), retrieved.getSet().iterator().next());
    assertEquals(1, retrieved.getMap().size());
    assertEquals(new ValueType("map_value"), retrieved.getMap().get(new ValueType("map_key")));
    s.delete(retrieved);
    s.getTransaction().commit();
    s.close();
}
Also used : IndexedCollection(org.hibernate.mapping.IndexedCollection) Collection(org.hibernate.mapping.Collection) ElementCollection(javax.persistence.ElementCollection) Property(org.hibernate.mapping.Property) IndexedCollection(org.hibernate.mapping.IndexedCollection) PersistentClass(org.hibernate.mapping.PersistentClass) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

IndexedCollection (org.hibernate.mapping.IndexedCollection)4 IdMappingData (org.hibernate.envers.internal.entities.IdMappingData)2 MiddleComponentData (org.hibernate.envers.internal.entities.mapper.relation.MiddleComponentData)2 ElementCollection (javax.persistence.ElementCollection)1 Session (org.hibernate.Session)1 PropertyData (org.hibernate.envers.internal.entities.PropertyData)1 MultiPropertyMapper (org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper)1 PropertyMapper (org.hibernate.envers.internal.entities.mapper.PropertyMapper)1 SinglePropertyMapper (org.hibernate.envers.internal.entities.mapper.SinglePropertyMapper)1 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)1 CommonCollectionMapperData (org.hibernate.envers.internal.entities.mapper.relation.CommonCollectionMapperData)1 MiddleIdData (org.hibernate.envers.internal.entities.mapper.relation.MiddleIdData)1 ToOneIdMapper (org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper)1 MiddleDummyComponentMapper (org.hibernate.envers.internal.entities.mapper.relation.component.MiddleDummyComponentMapper)1 MiddleMapKeyIdComponentMapper (org.hibernate.envers.internal.entities.mapper.relation.component.MiddleMapKeyIdComponentMapper)1 MiddleMapKeyPropertyComponentMapper (org.hibernate.envers.internal.entities.mapper.relation.component.MiddleMapKeyPropertyComponentMapper)1 MiddleRelatedComponentMapper (org.hibernate.envers.internal.entities.mapper.relation.component.MiddleRelatedComponentMapper)1 MiddleStraightComponentMapper (org.hibernate.envers.internal.entities.mapper.relation.component.MiddleStraightComponentMapper)1 OneAuditEntityQueryGenerator (org.hibernate.envers.internal.entities.mapper.relation.query.OneAuditEntityQueryGenerator)1 RelationQueryGenerator (org.hibernate.envers.internal.entities.mapper.relation.query.RelationQueryGenerator)1