Search in sources :

Example 86 with Collection

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

the class ListMappingTest method testOrderColumnInNormalBiDirectonalModel.

@Test
public void testOrderColumnInNormalBiDirectonalModel() {
    Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(Order.class).addAnnotatedClass(LineItem.class).buildMetadata();
    Collection lineItemsBinding = metadata.getCollectionBindings().iterator().next();
    // make sure it was interpreted as a List (aka, as having an OrderColumn at all)
    assertThat(lineItemsBinding, instanceOf(org.hibernate.mapping.List.class));
    org.hibernate.mapping.List asList = (org.hibernate.mapping.List) lineItemsBinding;
    // assert the OrderColumn details
    final Column positionColumn = (Column) asList.getIndex().getColumnIterator().next();
    assertThat(positionColumn.getName(), equalTo("position"));
    // make sure the OrderColumn is part of the collection table
    assertTrue(asList.getCollectionTable().containsColumn(positionColumn));
    class TargetImpl extends GenerationTargetToStdout {

        boolean found = false;

        @Override
        public void accept(String action) {
            super.accept(action);
            if (action.matches("^create( (column|row))? table t_line_item.+")) {
                if (action.contains("position")) {
                    found = true;
                }
            }
        }
    }
    TargetImpl target = new TargetImpl();
    new SchemaCreatorImpl(ssr).doCreation(metadata, true, target);
    assertTrue(target.found);
}
Also used : GenerationTargetToStdout(org.hibernate.tool.schema.internal.exec.GenerationTargetToStdout) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) JoinColumn(javax.persistence.JoinColumn) Column(org.hibernate.mapping.Column) OrderColumn(javax.persistence.OrderColumn) Collection(org.hibernate.mapping.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 87 with Collection

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

the class ModelBinder method createPluralAttribute.

private Property createPluralAttribute(MappingDocument sourceDocument, PluralAttributeSource attributeSource, PersistentClass entityDescriptor) {
    final Collection collectionBinding;
    if (attributeSource instanceof PluralAttributeSourceListImpl) {
        collectionBinding = new org.hibernate.mapping.List(sourceDocument, entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeListSecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (org.hibernate.mapping.List) collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceSetImpl) {
        collectionBinding = new Set(sourceDocument, entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeSetSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceMapImpl) {
        collectionBinding = new org.hibernate.mapping.Map(sourceDocument, entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeMapSecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (org.hibernate.mapping.Map) collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceBagImpl) {
        collectionBinding = new Bag(sourceDocument, entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeBagSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceIdBagImpl) {
        collectionBinding = new IdentifierBag(sourceDocument, entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeIdBagSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceArrayImpl) {
        final PluralAttributeSourceArray arraySource = (PluralAttributeSourceArray) attributeSource;
        collectionBinding = new Array(sourceDocument, entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        ((Array) collectionBinding).setElementClassName(sourceDocument.qualifyClassName(arraySource.getElementClass()));
        registerSecondPass(new PluralAttributeArraySecondPass(sourceDocument, arraySource, (Array) collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourcePrimitiveArrayImpl) {
        collectionBinding = new PrimitiveArray(sourceDocument, entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributePrimitiveArraySecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (PrimitiveArray) collectionBinding), sourceDocument);
    } else {
        throw new AssertionFailure("Unexpected PluralAttributeSource type : " + attributeSource.getClass().getName());
    }
    sourceDocument.getMetadataCollector().addCollectionBinding(collectionBinding);
    final Property attribute = new Property();
    attribute.setValue(collectionBinding);
    bindProperty(sourceDocument, attributeSource, attribute);
    return attribute;
}
Also used : Set(org.hibernate.mapping.Set) ArrayList(java.util.ArrayList) List(java.util.List) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty) AssertionFailure(org.hibernate.AssertionFailure) Bag(org.hibernate.mapping.Bag) IdentifierBag(org.hibernate.mapping.IdentifierBag) IdentifierBag(org.hibernate.mapping.IdentifierBag) PrimitiveArray(org.hibernate.mapping.PrimitiveArray) PluralAttributeSourceArray(org.hibernate.boot.model.source.spi.PluralAttributeSourceArray) Array(org.hibernate.mapping.Array) PluralAttributeSourceArray(org.hibernate.boot.model.source.spi.PluralAttributeSourceArray) PrimitiveArray(org.hibernate.mapping.PrimitiveArray) Collection(org.hibernate.mapping.Collection) IndexedCollection(org.hibernate.mapping.IndexedCollection) IdentifierCollection(org.hibernate.mapping.IdentifierCollection) Map(java.util.Map) HashMap(java.util.HashMap)

Example 88 with Collection

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

the class TestHelper method createRegions.

public static void createRegions(Metadata metadata, boolean queryRegions, boolean prefixRegions) {
    Set<String> names = new HashSet<>();
    final CacheManager cacheManager = locateStandardCacheManager();
    for (PersistentClass persistentClass : metadata.getEntityBindings()) {
        if (persistentClass.getRootClass().isCached()) {
            if (!names.add(persistentClass.getRootClass().getCacheRegionName())) {
                continue;
            }
            createCache(cacheManager, persistentClass.getRootClass().getCacheRegionName(), prefixRegions);
        }
        if (persistentClass.hasNaturalId()) {
            if (persistentClass.getNaturalIdCacheRegionName() != null) {
                if (!names.add(persistentClass.getNaturalIdCacheRegionName())) {
                    continue;
                }
                createCache(cacheManager, persistentClass.getNaturalIdCacheRegionName(), prefixRegions);
            }
        }
    }
    for (Collection collection : metadata.getCollectionBindings()) {
        if (collection.getCacheRegionName() == null || collection.getCacheConcurrencyStrategy() == null) {
            continue;
        }
        if (!names.add(collection.getCacheRegionName())) {
            continue;
        }
        createCache(cacheManager, collection.getCacheRegionName(), prefixRegions);
    }
    if (queryRegions) {
        createCache(cacheManager, TimestampsRegion.class.getName(), prefixRegions);
        createCache(cacheManager, QueryResultsRegion.class.getName(), prefixRegions);
    }
}
Also used : JCacheHelper.locateStandardCacheManager(org.hibernate.cache.jcache.JCacheHelper.locateStandardCacheManager) CacheManager(javax.cache.CacheManager) Collection(org.hibernate.mapping.Collection) HashSet(java.util.HashSet) PersistentClass(org.hibernate.mapping.PersistentClass) TimestampsRegion(org.hibernate.cache.spi.TimestampsRegion) QueryResultsRegion(org.hibernate.cache.spi.QueryResultsRegion)

Example 89 with Collection

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

the class AbstractPropertyMapping method addPropertyPath.

protected void addPropertyPath(String path, Type type, String[] columns, String[] columnReaders, String[] columnReaderTemplates, String[] formulaTemplates, Mapping factory) {
    Type existingType = typesByPropertyPath.get(path);
    if (existingType != null || duplicateIncompatiblePaths.contains(path)) {
        // If types match or the new type is not an association type, there is nothing for us to do
        if (type == existingType || existingType == null || !(type instanceof AssociationType)) {
            logDuplicateRegistration(path, existingType, type);
        } else if (!(existingType instanceof AssociationType)) {
            // Workaround for org.hibernate.cfg.annotations.PropertyBinder.bind() adding a component for *ToOne ids
            logDuplicateRegistration(path, existingType, type);
        } else {
            if (type instanceof AnyType && existingType instanceof AnyType) {
            // TODO: not sure how to handle any types. For now we just return and let the first type dictate what type the property has...
            } else {
                Type commonType = null;
                MetadataImplementor metadata = (MetadataImplementor) factory;
                if (type instanceof CollectionType && existingType instanceof CollectionType) {
                    Collection thisCollection = metadata.getCollectionBinding(((CollectionType) existingType).getRole());
                    Collection otherCollection = metadata.getCollectionBinding(((CollectionType) type).getRole());
                    if (thisCollection.isSame(otherCollection)) {
                        logDuplicateRegistration(path, existingType, type);
                        return;
                    } else {
                        logIncompatibleRegistration(path, existingType, type);
                    }
                } else if (type instanceof EntityType && existingType instanceof EntityType) {
                    EntityType entityType1 = (EntityType) existingType;
                    EntityType entityType2 = (EntityType) type;
                    if (entityType1.getAssociatedEntityName().equals(entityType2.getAssociatedEntityName())) {
                        logDuplicateRegistration(path, existingType, type);
                        return;
                    } else {
                        commonType = getCommonType(metadata, entityType1, entityType2);
                    }
                } else {
                    logIncompatibleRegistration(path, existingType, type);
                }
                if (commonType == null) {
                    duplicateIncompatiblePaths.add(path);
                    typesByPropertyPath.remove(path);
                    // Set everything to empty to signal action has to be taken!
                    // org.hibernate.hql.internal.ast.tree.DotNode.dereferenceEntityJoin() is reacting to this
                    String[] empty = new String[0];
                    columnsByPropertyPath.put(path, empty);
                    columnReadersByPropertyPath.put(path, empty);
                    columnReaderTemplatesByPropertyPath.put(path, empty);
                    if (formulaTemplates != null) {
                        formulaTemplatesByPropertyPath.put(path, empty);
                    }
                } else {
                    typesByPropertyPath.put(path, commonType);
                }
            }
        }
    } else {
        typesByPropertyPath.put(path, type);
        columnsByPropertyPath.put(path, columns);
        columnReadersByPropertyPath.put(path, columnReaders);
        columnReaderTemplatesByPropertyPath.put(path, columnReaderTemplates);
        if (formulaTemplates != null) {
            formulaTemplatesByPropertyPath.put(path, formulaTemplates);
        }
    }
}
Also used : EntityType(org.hibernate.type.EntityType) OneToOneType(org.hibernate.type.OneToOneType) AnyType(org.hibernate.type.AnyType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) ManyToOneType(org.hibernate.type.ManyToOneType) SpecialOneToOneType(org.hibernate.type.SpecialOneToOneType) CompositeType(org.hibernate.type.CompositeType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) CollectionType(org.hibernate.type.CollectionType) Collection(org.hibernate.mapping.Collection) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) AnyType(org.hibernate.type.AnyType)

Example 90 with Collection

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

the class MetamodelImpl method primeSecondLevelCacheRegions.

private void primeSecondLevelCacheRegions(MetadataImplementor mappingMetadata) {
    final Map<String, DomainDataRegionConfigImpl.Builder> regionConfigBuilders = new ConcurrentHashMap<>();
    for (PersistentClass bootEntityDescriptor : mappingMetadata.getEntityBindings()) {
        final AccessType accessType = AccessType.fromExternalName(bootEntityDescriptor.getCacheConcurrencyStrategy());
        if (accessType != null) {
            if (bootEntityDescriptor.isCached()) {
                regionConfigBuilders.computeIfAbsent(bootEntityDescriptor.getRootClass().getCacheRegionName(), DomainDataRegionConfigImpl.Builder::new).addEntityConfig(bootEntityDescriptor, accessType);
            }
            if (RootClass.class.isInstance(bootEntityDescriptor) && bootEntityDescriptor.hasNaturalId() && bootEntityDescriptor.getNaturalIdCacheRegionName() != null) {
                regionConfigBuilders.computeIfAbsent(bootEntityDescriptor.getNaturalIdCacheRegionName(), DomainDataRegionConfigImpl.Builder::new).addNaturalIdConfig((RootClass) bootEntityDescriptor, accessType);
            }
        }
    }
    for (Collection collection : mappingMetadata.getCollectionBindings()) {
        final AccessType accessType = AccessType.fromExternalName(collection.getCacheConcurrencyStrategy());
        if (accessType != null) {
            regionConfigBuilders.computeIfAbsent(collection.getCacheRegionName(), DomainDataRegionConfigImpl.Builder::new).addCollectionConfig(collection, accessType);
        }
    }
    final Set<DomainDataRegionConfig> regionConfigs;
    if (regionConfigBuilders.isEmpty()) {
        regionConfigs = Collections.emptySet();
    } else {
        regionConfigs = new HashSet<>();
        for (DomainDataRegionConfigImpl.Builder builder : regionConfigBuilders.values()) {
            regionConfigs.add(builder.build());
        }
    }
    getSessionFactory().getCache().prime(regionConfigs);
}
Also used : DomainDataRegionConfigImpl(org.hibernate.cache.cfg.internal.DomainDataRegionConfigImpl) Collection(org.hibernate.mapping.Collection) DomainDataRegionConfig(org.hibernate.cache.cfg.spi.DomainDataRegionConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AccessType(org.hibernate.cache.spi.access.AccessType) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

Collection (org.hibernate.mapping.Collection)134 Test (org.junit.jupiter.api.Test)80 Bag (org.hibernate.mapping.Bag)61 IdentifierBag (org.hibernate.mapping.IdentifierBag)61 SimpleValue (org.hibernate.mapping.SimpleValue)28 Table (org.hibernate.mapping.Table)27 Set (org.hibernate.mapping.Set)25 ITable (org.jboss.tools.hibernate.runtime.spi.ITable)24 Test (org.junit.Test)23 PersistentClass (org.hibernate.mapping.PersistentClass)22 ManyToOne (org.hibernate.mapping.ManyToOne)14 KeyValue (org.hibernate.mapping.KeyValue)13 IValue (org.jboss.tools.hibernate.runtime.spi.IValue)12 Property (org.hibernate.mapping.Property)11 Metadata (org.hibernate.boot.Metadata)9 MetadataSources (org.hibernate.boot.MetadataSources)9 Column (org.hibernate.mapping.Column)9 Iterator (java.util.Iterator)7 Component (org.hibernate.mapping.Component)7 AbstractValueFacade (org.jboss.tools.hibernate.runtime.common.AbstractValueFacade)7