Search in sources :

Example 36 with MappingMetamodelImplementor

use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.

the class StatefulPersistenceContext method getOwnerId.

@Override
public Object getOwnerId(String entityName, String propertyName, Object childEntity, Map mergeMap) {
    final String collectionRole = entityName + '.' + propertyName;
    final MappingMetamodelImplementor mappingMetamodel = session.getFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister persister = mappingMetamodel.getEntityDescriptor(entityName);
    final CollectionPersister collectionPersister = mappingMetamodel.getCollectionDescriptor(collectionRole);
    // try cache lookup first
    final Object parent = getParentsByChild(childEntity);
    if (parent != null) {
        final EntityEntry entityEntry = entityEntryContext.getEntityEntry(parent);
        // there maybe more than one parent, filter by type
        if (persister.isSubclassEntityName(entityEntry.getEntityName()) && isFoundInParent(propertyName, childEntity, persister, collectionPersister, parent)) {
            return getEntry(parent).getId();
        } else {
            // remove wrong entry
            removeChildParent(childEntity);
        }
    }
    // iterate all the entities currently associated with the persistence context.
    for (Entry<Object, EntityEntry> me : reentrantSafeEntityEntries()) {
        final EntityEntry entityEntry = me.getValue();
        // does this entity entry pertain to the entity persister in which we are interested (owner)?
        if (persister.isSubclassEntityName(entityEntry.getEntityName())) {
            final Object entityEntryInstance = me.getKey();
            // check if the managed object is the parent
            boolean found = isFoundInParent(propertyName, childEntity, persister, collectionPersister, entityEntryInstance);
            if (!found && mergeMap != null) {
                // check if the detached object being merged is the parent
                final Object unmergedInstance = mergeMap.get(entityEntryInstance);
                final Object unmergedChild = mergeMap.get(childEntity);
                if (unmergedInstance != null && unmergedChild != null) {
                    found = isFoundInParent(propertyName, unmergedChild, persister, collectionPersister, unmergedInstance);
                    LOG.debugf("Detached object being merged (corresponding with a managed entity) has a collection that [%s] the detached child.", (found ? "contains" : "does not contain"));
                }
            }
            if (found) {
                return entityEntry.getId();
            }
        }
    }
    // of the loop-in-loop especially considering this is far more likely the 'edge case'
    if (mergeMap != null) {
        for (Object o : mergeMap.entrySet()) {
            final Entry<?, ?> mergeMapEntry = (Entry<?, ?>) o;
            if (mergeMapEntry.getKey() instanceof HibernateProxy) {
                final HibernateProxy proxy = (HibernateProxy) mergeMapEntry.getKey();
                if (persister.isSubclassEntityName(proxy.getHibernateLazyInitializer().getEntityName())) {
                    boolean found = isFoundInParent(propertyName, childEntity, persister, collectionPersister, mergeMap.get(proxy));
                    LOG.debugf("Detached proxy being merged has a collection that [%s] the managed child.", (found ? "contains" : "does not contain"));
                    if (!found) {
                        found = isFoundInParent(propertyName, mergeMap.get(childEntity), persister, collectionPersister, mergeMap.get(proxy));
                        LOG.debugf("Detached proxy being merged has a collection that [%s] the detached child being merged..", (found ? "contains" : "does not contain"));
                    }
                    if (found) {
                        return proxy.getHibernateLazyInitializer().getInternalIdentifier();
                    }
                }
            }
        }
    }
    return null;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityEntry(org.hibernate.engine.spi.EntityEntry) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) Entry(java.util.Map.Entry) EntityEntry(org.hibernate.engine.spi.EntityEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 37 with MappingMetamodelImplementor

use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.

the class StatefulPersistenceContext method getIndexInOwner.

@Override
public Object getIndexInOwner(String entity, String property, Object childEntity, Map mergeMap) {
    final MappingMetamodelImplementor metamodel = session.getFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister persister = metamodel.getEntityDescriptor(entity);
    final CollectionPersister cp = metamodel.getCollectionDescriptor(entity + '.' + property);
    // try cache lookup first
    final Object parent = getParentsByChild(childEntity);
    if (parent != null) {
        final EntityEntry entityEntry = entityEntryContext.getEntityEntry(parent);
        // there maybe more than one parent, filter by type
        if (persister.isSubclassEntityName(entityEntry.getEntityName())) {
            Object index = getIndexInParent(property, childEntity, persister, cp, parent);
            if (index == null && mergeMap != null) {
                final Object unMergedInstance = mergeMap.get(parent);
                final Object unMergedChild = mergeMap.get(childEntity);
                if (unMergedInstance != null && unMergedChild != null) {
                    index = getIndexInParent(property, unMergedChild, persister, cp, unMergedInstance);
                    LOG.debugf("A detached object being merged (corresponding to a parent in parentsByChild) has an indexed collection that [%s] the detached child being merged. ", (index != null ? "contains" : "does not contain"));
                }
            }
            if (index != null) {
                return index;
            }
        } else {
            // remove wrong entry
            removeChildParent(childEntity);
        }
    }
    // Not found in cache, proceed
    for (Entry<Object, EntityEntry> me : reentrantSafeEntityEntries()) {
        final EntityEntry ee = me.getValue();
        if (persister.isSubclassEntityName(ee.getEntityName())) {
            final Object instance = me.getKey();
            Object index = getIndexInParent(property, childEntity, persister, cp, instance);
            if (index == null && mergeMap != null) {
                final Object unMergedInstance = mergeMap.get(instance);
                final Object unMergedChild = mergeMap.get(childEntity);
                if (unMergedInstance != null && unMergedChild != null) {
                    index = getIndexInParent(property, unMergedChild, persister, cp, unMergedInstance);
                    LOG.debugf("A detached object being merged (corresponding to a managed entity) has an indexed collection that [%s] the detached child being merged. ", (index != null ? "contains" : "does not contain"));
                }
            }
            if (index != null) {
                return index;
            }
        }
    }
    return null;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityEntry(org.hibernate.engine.spi.EntityEntry) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor)

Example 38 with MappingMetamodelImplementor

use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.

the class BasicArrayMappingTests method testMappings.

@Test
public void testMappings(SessionFactoryScope scope) {
    // first, verify the type selections...
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(EntityOfArrays.class);
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapper");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(Short[].class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Short[].class));
    }
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("primitive");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(short[].class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(short[].class));
    }
    // and try to use the mapping
    scope.inTransaction((session) -> session.persist(new EntityOfArrays(1, new Short[] { (short) 3 }, new short[] { (short) 5 })));
    scope.inTransaction((session) -> session.get(EntityOfArrays.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Test(org.junit.jupiter.api.Test)

Example 39 with MappingMetamodelImplementor

use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.

the class BigDecimalMappingTests method testMappings.

@Test
public void testMappings(SessionFactoryScope scope) {
    // first, verify the type selections...
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.getEntityDescriptor(EntityOfBigDecimals.class);
    final JdbcTypeRegistry jdbcTypeRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry();
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapper");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(BigDecimal.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(BigDecimal.class));
        assertThat(jdbcMapping.getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.NUMERIC)));
    }
    // and try to use the mapping
    scope.inTransaction((session) -> session.persist(new EntityOfBigDecimals(1, BigDecimal.TEN)));
    scope.inTransaction((session) -> session.get(EntityOfBigDecimals.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) Test(org.junit.jupiter.api.Test)

Example 40 with MappingMetamodelImplementor

use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.

the class BigIntegerMappingTests method testMappings.

@Test
public void testMappings(SessionFactoryScope scope) {
    // first, verify the type selections...
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.getEntityDescriptor(EntityOfBigIntegers.class);
    final JdbcTypeRegistry jdbcTypeRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry();
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapper");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(BigInteger.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(BigInteger.class));
        assertThat(jdbcMapping.getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.NUMERIC)));
    }
    // and try to use the mapping
    scope.inTransaction((session) -> session.persist(new EntityOfBigIntegers(1, BigInteger.TEN)));
    scope.inTransaction((session) -> session.get(EntityOfBigIntegers.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) Test(org.junit.jupiter.api.Test)

Aggregations

MappingMetamodelImplementor (org.hibernate.metamodel.spi.MappingMetamodelImplementor)55 EntityPersister (org.hibernate.persister.entity.EntityPersister)46 Test (org.junit.jupiter.api.Test)43 BasicAttributeMapping (org.hibernate.metamodel.mapping.internal.BasicAttributeMapping)42 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)33 JdbcTypeRegistry (org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry)18 BitSet (java.util.BitSet)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)4 JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)4 List (java.util.List)3 Map (java.util.Map)3 Dialect (org.hibernate.dialect.Dialect)3 Type (org.hibernate.type.Type)3 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 EntityNameResolver (org.hibernate.EntityNameResolver)2 HibernateException (org.hibernate.HibernateException)2 CollectionDataAccess (org.hibernate.cache.spi.access.CollectionDataAccess)2