Search in sources :

Example 91 with Collection

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

the class MetamodelImpl method initialize.

/**
 * Prepare the metamodel using the information from the collection of Hibernate
 * {@link PersistentClass} models
 *
 * @param mappingMetadata The mapping information
 * @param jpaMetaModelPopulationSetting Should the JPA Metamodel be built as well?
 */
public void initialize(MetadataImplementor mappingMetadata, JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting) {
    this.imports.putAll(mappingMetadata.getImports());
    primeSecondLevelCacheRegions(mappingMetadata);
    final PersisterCreationContext persisterCreationContext = new PersisterCreationContext() {

        @Override
        public SessionFactoryImplementor getSessionFactory() {
            return sessionFactory;
        }

        @Override
        public MetadataImplementor getMetadata() {
            return mappingMetadata;
        }
    };
    final PersisterFactory persisterFactory = sessionFactory.getServiceRegistry().getService(PersisterFactory.class);
    for (final PersistentClass model : mappingMetadata.getEntityBindings()) {
        final NavigableRole rootEntityRole = new NavigableRole(model.getRootClass().getEntityName());
        final EntityDataAccess accessStrategy = sessionFactory.getCache().getEntityRegionAccess(rootEntityRole);
        final NaturalIdDataAccess naturalIdAccessStrategy = sessionFactory.getCache().getNaturalIdCacheRegionAccessStrategy(rootEntityRole);
        final EntityPersister cp = persisterFactory.createEntityPersister(model, accessStrategy, naturalIdAccessStrategy, persisterCreationContext);
        entityPersisterMap.put(model.getEntityName(), cp);
        if (cp.getConcreteProxyClass() != null && cp.getConcreteProxyClass().isInterface() && !Map.class.isAssignableFrom(cp.getConcreteProxyClass()) && cp.getMappedClass() != cp.getConcreteProxyClass()) {
            if (cp.getMappedClass().equals(cp.getConcreteProxyClass())) {
                // this part handles an odd case in the Hibernate test suite where we map an interface
                // as the class and the proxy.  I cannot think of a real life use case for that
                // specific test, but..
                log.debugf("Entity [%s] mapped same interface [%s] as class and proxy", cp.getEntityName(), cp.getMappedClass());
            } else {
                final String old = entityProxyInterfaceMap.put(cp.getConcreteProxyClass(), cp.getEntityName());
                if (old != null) {
                    throw new HibernateException(String.format(Locale.ENGLISH, "Multiple entities [%s, %s] named the same interface [%s] as their proxy which is not supported", old, cp.getEntityName(), cp.getConcreteProxyClass().getName()));
                }
            }
        }
    }
    for (final Collection model : mappingMetadata.getCollectionBindings()) {
        final NavigableRole navigableRole = new NavigableRole(model.getRole());
        final CollectionDataAccess accessStrategy = sessionFactory.getCache().getCollectionRegionAccess(navigableRole);
        final CollectionPersister persister = persisterFactory.createCollectionPersister(model, accessStrategy, persisterCreationContext);
        collectionPersisterMap.put(model.getRole(), persister);
        Type indexType = persister.getIndexType();
        if (indexType != null && indexType.isAssociationType() && !indexType.isAnyType()) {
            String entityName = ((AssociationType) indexType).getAssociatedEntityName(sessionFactory);
            Set<String> roles = collectionRolesByEntityParticipant.get(entityName);
            if (roles == null) {
                roles = new HashSet<>();
                collectionRolesByEntityParticipant.put(entityName, roles);
            }
            roles.add(persister.getRole());
        }
        Type elementType = persister.getElementType();
        if (elementType.isAssociationType() && !elementType.isAnyType()) {
            String entityName = ((AssociationType) elementType).getAssociatedEntityName(sessionFactory);
            Set<String> roles = collectionRolesByEntityParticipant.get(entityName);
            if (roles == null) {
                roles = new HashSet<>();
                collectionRolesByEntityParticipant.put(entityName, roles);
            }
            roles.add(persister.getRole());
        }
    }
    // after *all* persisters and named queries are registered
    entityPersisterMap.values().forEach(EntityPersister::generateEntityDefinition);
    for (EntityPersister persister : entityPersisterMap.values()) {
        persister.postInstantiate();
        registerEntityNameResolvers(persister, entityNameResolvers);
    }
    collectionPersisterMap.values().forEach(CollectionPersister::postInstantiate);
    if (jpaMetaModelPopulationSetting != JpaMetaModelPopulationSetting.DISABLED) {
        MetadataContext context = new MetadataContext(sessionFactory, mappingMetadata.getMappedSuperclassMappingsCopy(), jpaMetaModelPopulationSetting);
        for (PersistentClass entityBinding : mappingMetadata.getEntityBindings()) {
            locateOrBuildEntityType(entityBinding, context);
        }
        handleUnusedMappedSuperclasses(context);
        context.wrapUp();
        this.jpaEntityTypeMap.putAll(context.getEntityTypeMap());
        this.jpaEmbeddableTypeMap.putAll(context.getEmbeddableTypeMap());
        this.jpaMappedSuperclassTypeMap.putAll(context.getMappedSuperclassTypeMap());
        this.jpaEntityTypesByEntityName.putAll(context.getEntityTypesByEntityName());
        applyNamedEntityGraphs(mappingMetadata.getNamedEntityGraphs().values());
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) HibernateException(org.hibernate.HibernateException) PersisterFactory(org.hibernate.persister.spi.PersisterFactory) NaturalIdDataAccess(org.hibernate.cache.spi.access.NaturalIdDataAccess) CollectionDataAccess(org.hibernate.cache.spi.access.CollectionDataAccess) PersisterCreationContext(org.hibernate.persister.spi.PersisterCreationContext) AccessType(org.hibernate.cache.spi.access.AccessType) ManagedType(javax.persistence.metamodel.ManagedType) MappedSuperclassType(javax.persistence.metamodel.MappedSuperclassType) EntityType(javax.persistence.metamodel.EntityType) AssociationType(org.hibernate.type.AssociationType) EmbeddableType(javax.persistence.metamodel.EmbeddableType) Type(org.hibernate.type.Type) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) AssociationType(org.hibernate.type.AssociationType) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) Collection(org.hibernate.mapping.Collection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) PersistentClass(org.hibernate.mapping.PersistentClass) EntityDataAccess(org.hibernate.cache.spi.access.EntityDataAccess)

Example 92 with Collection

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

the class ValueFacadeTest method testSetKey.

@Test
public void testSetKey() {
    KeyValue keyValueTarget = new SimpleValue();
    IValue keyValueFacade = FACADE_FACTORY.createValue(keyValueTarget);
    Collection collectionTarget = new Bag(null);
    IValue collectionFacade = FACADE_FACTORY.createValue(collectionTarget);
    Assert.assertNull(collectionTarget.getKey());
    collectionFacade.setKey(keyValueFacade);
    Assert.assertSame(keyValueTarget, collectionTarget.getKey());
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) IValue(org.jboss.tools.hibernate.runtime.spi.IValue) Collection(org.hibernate.mapping.Collection) Bag(org.hibernate.mapping.Bag) IdentifierBag(org.hibernate.mapping.IdentifierBag) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.Test)

Example 93 with Collection

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

the class ConfigurationFacadeTest method testBuildMappings.

@Test
public void testBuildMappings() throws Exception {
    File fooFile = File.createTempFile("foo", "hbm.xml");
    PrintWriter fooWriter = new PrintWriter(fooFile);
    fooWriter.write(FOO_HBM_XML_STRING);
    fooWriter.close();
    configuration.addFile(fooFile);
    File barFile = File.createTempFile("bar", "hbm.xml");
    PrintWriter barWriter = new PrintWriter(barFile);
    barWriter.write(BAR_HBM_XML_STRING);
    barWriter.close();
    configuration.addFile(barFile);
    String collectionName = "org.jboss.tools.hibernate.runtime.v_3_6.internal.ConfigurationFacadeTest$Bar.fooSet";
    assertNull(configuration.getCollectionMapping(collectionName));
    configurationFacade.buildMappings();
    Collection collection = configuration.getCollectionMapping(collectionName);
    OneToMany element = (OneToMany) collection.getElement();
    assertEquals("org.jboss.tools.hibernate.runtime.v_3_6.internal.ConfigurationFacadeTest$Foo", element.getAssociatedClass().getClassName());
}
Also used : Collection(org.hibernate.mapping.Collection) OneToMany(org.hibernate.mapping.OneToMany) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.jupiter.api.Test)

Example 94 with Collection

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

the class ValueFacadeTest method testSetRole.

@Test
public void testSetRole() {
    Collection collectionTarget = new Bag(null, null);
    valueFacade = FACADE_FACTORY.createValue(collectionTarget);
    assertNull(collectionTarget.getRole());
    valueFacade.setRole("foobar");
    assertEquals("foobar", collectionTarget.getRole());
}
Also used : Collection(org.hibernate.mapping.Collection) Bag(org.hibernate.mapping.Bag) IdentifierBag(org.hibernate.mapping.IdentifierBag) Test(org.junit.jupiter.api.Test)

Example 95 with Collection

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

the class ValueFacadeTest method testSetLazy.

@Test
public void testSetLazy() {
    Collection collectionTarget = new Bag(null, null);
    valueFacade = FACADE_FACTORY.createValue(collectionTarget);
    valueFacade.setLazy(true);
    assertTrue(collectionTarget.isLazy());
    valueFacade.setLazy(false);
    assertFalse(collectionTarget.isLazy());
}
Also used : Collection(org.hibernate.mapping.Collection) Bag(org.hibernate.mapping.Bag) IdentifierBag(org.hibernate.mapping.IdentifierBag) Test(org.junit.jupiter.api.Test)

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