Search in sources :

Example 26 with MappingMetamodelImplementor

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

the class ShortMappingTests 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(EntityOfShorts.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));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), is(Types.SMALLINT));
    }
    {
        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));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), is(Types.SMALLINT));
    }
    // and try to use the mapping
    scope.inTransaction((session) -> session.persist(new EntityOfShorts(1, (short) 3, (short) 5)));
    scope.inTransaction((session) -> session.get(EntityOfShorts.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 27 with MappingMetamodelImplementor

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

the class ZoneOffsetMappingTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final JdbcTypeRegistry jdbcRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(EntityWithZoneOffset.class);
    final BasicAttributeMapping duration = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("zoneOffset");
    final JdbcMapping jdbcMapping = duration.getJdbcMapping();
    assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(ZoneOffset.class));
    assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.VARCHAR)));
    scope.inTransaction((session) -> {
        session.persist(new EntityWithZoneOffset(1, ZoneOffset.from(ZoneOffset.MIN)));
    });
    scope.inTransaction((session) -> session.find(EntityWithZoneOffset.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) ZoneOffset(java.time.ZoneOffset) Test(org.junit.jupiter.api.Test)

Example 28 with MappingMetamodelImplementor

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

the class BitSetConverterImmutableTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(Product.class);
    final BasicAttributeMapping attributeMapping = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("bitSet");
    assertThat(attributeMapping.getJavaType().getJavaTypeClass(), equalTo(BitSet.class));
    assertThat(attributeMapping.getValueConverter(), instanceOf(JpaAttributeConverter.class));
    final JpaAttributeConverter converter = (JpaAttributeConverter) attributeMapping.getValueConverter();
    assertThat(converter.getConverterBean().getBeanClass(), equalTo(BitSetConverter.class));
    Assertions.assertThat(attributeMapping.getExposedMutabilityPlan()).isNotInstanceOf(BitSetMutabilityPlan.class);
    Assertions.assertThat(attributeMapping.getExposedMutabilityPlan()).isInstanceOf(ImmutableMutabilityPlan.class);
    Assertions.assertThat(attributeMapping.getExposedMutabilityPlan().isMutable()).isFalse();
    final BitSet sample = new BitSet();
    Assertions.assertThat(((MutabilityPlan) attributeMapping.getExposedMutabilityPlan()).deepCopy(sample)).isSameAs(sample);
    assertThat(attributeMapping.getJdbcMapping().getJdbcType().getJdbcTypeCode(), isOneOf(Types.VARCHAR, Types.NVARCHAR));
    assertThat(attributeMapping.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass(), equalTo(String.class));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) JpaAttributeConverter(org.hibernate.metamodel.model.convert.spi.JpaAttributeConverter) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) BitSet(java.util.BitSet) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) MutabilityPlan(org.hibernate.type.descriptor.java.MutabilityPlan) ImmutableMutabilityPlan(org.hibernate.type.descriptor.java.ImmutableMutabilityPlan) Test(org.junit.jupiter.api.Test)

Example 29 with MappingMetamodelImplementor

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

the class BulkOperationCleanupAction method schedule.

public static void schedule(SharedSessionContractImplementor session, SqmDmlStatement<?> statement) {
    final List<EntityPersister> entityPersisters = new ArrayList<>(1);
    final MappingMetamodelImplementor metamodel = session.getFactory().getRuntimeMetamodels().getMappingMetamodel();
    if (!(statement instanceof InsertStatement)) {
        entityPersisters.add(metamodel.getEntityDescriptor(statement.getTarget().getEntityName()));
    }
    for (SqmCteStatement<?> cteStatement : statement.getCteStatements()) {
        final SqmStatement<?> cteDefinition = cteStatement.getCteDefinition();
        if (cteDefinition instanceof SqmDmlStatement<?> && !(cteDefinition instanceof InsertStatement)) {
            entityPersisters.add(metamodel.getEntityDescriptor(((SqmDmlStatement<?>) cteDefinition).getTarget().getEntityName()));
        }
    }
    schedule(session, entityPersisters.toArray(new EntityPersister[0]));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ArrayList(java.util.ArrayList) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) InsertStatement(org.hibernate.sql.ast.tree.insert.InsertStatement)

Example 30 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)

Aggregations

MappingMetamodelImplementor (org.hibernate.metamodel.spi.MappingMetamodelImplementor)49 EntityPersister (org.hibernate.persister.entity.EntityPersister)40 Test (org.junit.jupiter.api.Test)37 BasicAttributeMapping (org.hibernate.metamodel.mapping.internal.BasicAttributeMapping)36 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)29 JdbcTypeRegistry (org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry)14 BitSet (java.util.BitSet)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)4 Dialect (org.hibernate.dialect.Dialect)3 Type (org.hibernate.type.Type)3 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 SoftLock (org.hibernate.cache.spi.access.SoftLock)2 EntityEntry (org.hibernate.engine.spi.EntityEntry)2 JpaAttributeConverter (org.hibernate.metamodel.model.convert.spi.JpaAttributeConverter)2 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)2