Search in sources :

Example 26 with Collection

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

the class SubselectFetchWithFormulaTransactSqlTest method checkSubselectWithFormula.

@Test
public void checkSubselectWithFormula() throws Exception {
    // as a pre-condition make sure that subselect fetching is enabled for the collection...
    Collection collectionBinding = metadata().getCollectionBinding(Name.class.getName() + ".values");
    assertThat(collectionBinding.isSubselectLoadable(), is(true));
    // Now force the subselect fetch and make sure we do not get SQL errors
    Session session = openSession();
    session.getTransaction().begin();
    List results = session.createCriteria(Name.class).list();
    for (Object result : results) {
        Name name = (Name) result;
        name.getValues().size();
    }
    session.getTransaction().commit();
    session.close();
}
Also used : Collection(org.hibernate.mapping.Collection) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 27 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.getMetadataCollector(), 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.getMetadataCollector(), entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeSetSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceMapImpl) {
        collectionBinding = new org.hibernate.mapping.Map(sourceDocument.getMetadataCollector(), 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.getMetadataCollector(), entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeBagSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceIdBagImpl) {
        collectionBinding = new IdentifierBag(sourceDocument.getMetadataCollector(), 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.getMetadataCollector(), 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.getMetadataCollector(), 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 28 with Collection

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

the class DefaultNamingCollectionElementTest method isCollectionColumnPresent.

private void isCollectionColumnPresent(String collectionOwner, String propertyName, String columnName) {
    final Collection collection = metadata().getCollectionBinding(collectionOwner + "." + propertyName);
    final Iterator columnIterator = collection.getCollectionTable().getColumnIterator();
    boolean hasDefault = false;
    while (columnIterator.hasNext()) {
        Column column = (Column) columnIterator.next();
        if (columnName.equals(column.getName()))
            hasDefault = true;
    }
    assertTrue("Could not find " + columnName, hasDefault);
}
Also used : Column(org.hibernate.mapping.Column) Iterator(java.util.Iterator) Collection(org.hibernate.mapping.Collection)

Example 29 with Collection

use of org.hibernate.mapping.Collection 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)

Example 30 with Collection

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

the class InFlightMetadataCollectorImpl method processCachingOverrides.

private void processCachingOverrides() {
    if (options.getCacheRegionDefinitions() == null) {
        return;
    }
    for (CacheRegionDefinition cacheRegionDefinition : options.getCacheRegionDefinitions()) {
        if (cacheRegionDefinition.getRegionType() == CacheRegionDefinition.CacheRegionType.ENTITY) {
            final PersistentClass entityBinding = getEntityBinding(cacheRegionDefinition.getRole());
            if (entityBinding == null) {
                throw new HibernateException("Cache override referenced an unknown entity : " + cacheRegionDefinition.getRole());
            }
            if (!RootClass.class.isInstance(entityBinding)) {
                throw new HibernateException("Cache override referenced a non-root entity : " + cacheRegionDefinition.getRole());
            }
            ((RootClass) entityBinding).setCacheRegionName(cacheRegionDefinition.getRegion());
            ((RootClass) entityBinding).setCacheConcurrencyStrategy(cacheRegionDefinition.getUsage());
            ((RootClass) entityBinding).setLazyPropertiesCacheable(cacheRegionDefinition.isCacheLazy());
        } else if (cacheRegionDefinition.getRegionType() == CacheRegionDefinition.CacheRegionType.COLLECTION) {
            final Collection collectionBinding = getCollectionBinding(cacheRegionDefinition.getRole());
            if (collectionBinding == null) {
                throw new HibernateException("Cache override referenced an unknown collection role : " + cacheRegionDefinition.getRole());
            }
            collectionBinding.setCacheRegionName(cacheRegionDefinition.getRegion());
            collectionBinding.setCacheConcurrencyStrategy(cacheRegionDefinition.getUsage());
        }
    }
}
Also used : RootClass(org.hibernate.mapping.RootClass) HibernateException(org.hibernate.HibernateException) CacheRegionDefinition(org.hibernate.boot.CacheRegionDefinition) Collection(org.hibernate.mapping.Collection) IdentifierCollection(org.hibernate.mapping.IdentifierCollection) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

Collection (org.hibernate.mapping.Collection)42 PersistentClass (org.hibernate.mapping.PersistentClass)19 Test (org.junit.Test)15 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 RootClass (org.hibernate.mapping.RootClass)6 AnnotationException (org.hibernate.AnnotationException)5 SimpleValue (org.hibernate.mapping.SimpleValue)5 TestForIssue (org.hibernate.testing.TestForIssue)5 List (java.util.List)4 Map (java.util.Map)4 ElementCollection (javax.persistence.ElementCollection)4 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)4 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 Value (org.hibernate.mapping.Value)4 ArrayList (java.util.ArrayList)3