Search in sources :

Example 1 with EntityIdentifierMapping

use of org.hibernate.metamodel.mapping.EntityIdentifierMapping in project hibernate-orm by hibernate.

the class DynamicCompositeIdBasicBindingTests method testBinding.

@Test
public void testBinding(ServiceRegistryScope scope) {
    final SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) new MetadataSources(scope.getRegistry()).addResource("org/hibernate/orm/test/bootstrap/binding/hbm/cid/nonaggregated/dynamic/DynamicCompositeIdBasic.hbm.xml").buildMetadata().buildSessionFactory();
    try {
        final EntityPersister entityDescriptor = sessionFactory.getRuntimeMetamodels().getMappingMetamodel().findEntityDescriptor("DynamicCompositeIdBasic");
        assertThat(entityDescriptor.getNumberOfAttributeMappings(), is(1));
        final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping();
        assertThat(identifierMapping, instanceOf(EmbeddedIdentifierMappingImpl.class));
        final EmbeddedIdentifierMappingImpl cid = (EmbeddedIdentifierMappingImpl) identifierMapping;
        assertThat(cid.getEmbeddableTypeDescriptor().getNumberOfAttributeMappings(), is(2));
        final AttributeMapping key1 = cid.getEmbeddableTypeDescriptor().findAttributeMapping("key1");
        assertThat(key1, notNullValue());
        final AttributeMapping key2 = cid.getEmbeddableTypeDescriptor().findAttributeMapping("key2");
        assertThat(key2, notNullValue());
        final AttributeMapping attr1 = entityDescriptor.findAttributeMapping("attr1");
        assertThat(attr1, notNullValue());
    } finally {
        sessionFactory.close();
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) MetadataSources(org.hibernate.boot.MetadataSources) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) EmbeddedIdentifierMappingImpl(org.hibernate.metamodel.mapping.internal.EmbeddedIdentifierMappingImpl) Test(org.junit.jupiter.api.Test)

Example 2 with EntityIdentifierMapping

use of org.hibernate.metamodel.mapping.EntityIdentifierMapping in project hibernate-orm by hibernate.

the class SimpleHbmTests method testBinding.

@Test
public void testBinding(ServiceRegistryScope scope) {
    final SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) new MetadataSources(scope.getRegistry()).addResource("org/hibernate/orm/test/bootstrap/binding/hbm/simple/pojo/SimpleEntity.hbm.xml").buildMetadata().buildSessionFactory();
    final EntityPersister entityDescriptor = sessionFactory.getRuntimeMetamodels().getMappingMetamodel().findEntityDescriptor(SimpleEntity.class);
    final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping();
    assertThat(identifierMapping, instanceOf(BasicEntityIdentifierMapping.class));
    final BasicEntityIdentifierMapping bid = (BasicEntityIdentifierMapping) identifierMapping;
    assertThat(bid.getFetchableName(), is("id"));
    assertThat(bid.getPartName(), is(EntityIdentifierMapping.ROLE_LOCAL_NAME));
    assertThat(entityDescriptor.getNumberOfAttributeMappings(), is(1));
    assertThat(entityDescriptor.getNumberOfDeclaredAttributeMappings(), is(1));
    final AttributeMapping nameAttr = entityDescriptor.findAttributeMapping("name");
    assertThat(nameAttr, notNullValue());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) MetadataSources(org.hibernate.boot.MetadataSources) BasicEntityIdentifierMapping(org.hibernate.metamodel.mapping.BasicEntityIdentifierMapping) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) BasicEntityIdentifierMapping(org.hibernate.metamodel.mapping.BasicEntityIdentifierMapping) Test(org.junit.jupiter.api.Test)

Example 3 with EntityIdentifierMapping

use of org.hibernate.metamodel.mapping.EntityIdentifierMapping in project hibernate-orm by hibernate.

the class DiscriminatedAssociationAttributeMapping method disassemble.

@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
    if (value == null) {
        return null;
    }
    final EntityMappingType concreteMappingType = determineConcreteType(value, session);
    final EntityIdentifierMapping identifierMapping = concreteMappingType.getIdentifierMapping();
    final Object discriminator = discriminatorMapping.getModelPart().resolveDiscriminatorForEntityType(concreteMappingType);
    final Object identifier = identifierMapping.getIdentifier(value);
    return new Object[] { discriminatorMapping.getDiscriminatorPart().disassemble(discriminator, session), identifierMapping.disassemble(identifier, session) };
}
Also used : EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 4 with EntityIdentifierMapping

use of org.hibernate.metamodel.mapping.EntityIdentifierMapping in project hibernate-orm by hibernate.

the class DiscriminatedAssociationAttributeMapping method breakDownJdbcValues.

@Override
public void breakDownJdbcValues(Object domainValue, JdbcValueConsumer valueConsumer, SharedSessionContractImplementor session) {
    final EntityMappingType concreteMappingType = determineConcreteType(domainValue, session);
    final Object discriminator = discriminatorMapping.getModelPart().resolveDiscriminatorForEntityType(concreteMappingType);
    final Object disassembledDiscriminator = discriminatorMapping.getDiscriminatorPart().disassemble(discriminator, session);
    valueConsumer.consume(disassembledDiscriminator, discriminatorMapping.getDiscriminatorPart());
    final EntityIdentifierMapping identifierMapping = concreteMappingType.getIdentifierMapping();
    final Object identifier = identifierMapping.getIdentifier(domainValue);
    final Object disassembledKey = discriminatorMapping.getKeyPart().disassemble(identifier, session);
    valueConsumer.consume(disassembledKey, discriminatorMapping.getKeyPart());
}
Also used : EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 5 with EntityIdentifierMapping

use of org.hibernate.metamodel.mapping.EntityIdentifierMapping in project hibernate-orm by hibernate.

the class DiscriminatedAssociationAttributeMapping method forEachDisassembledJdbcValue.

@Override
public int forEachDisassembledJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) {
    if (value != null) {
        if (value.getClass().isArray()) {
            final Object[] values = (Object[]) value;
            valuesConsumer.consume(offset, values[0], discriminatorMapping.getDiscriminatorPart().getJdbcMapping());
            valuesConsumer.consume(offset + 1, values[1], discriminatorMapping.getKeyPart().getJdbcMapping());
        } else {
            final EntityMappingType concreteMappingType = determineConcreteType(value, session);
            final Object discriminator = discriminatorMapping.getModelPart().resolveDiscriminatorForEntityType(concreteMappingType);
            final Object disassembledDiscriminator = discriminatorMapping.getDiscriminatorPart().disassemble(discriminator, session);
            valuesConsumer.consume(offset, disassembledDiscriminator, discriminatorMapping.getDiscriminatorPart().getJdbcMapping());
            final EntityIdentifierMapping identifierMapping = concreteMappingType.getIdentifierMapping();
            final Object identifier = identifierMapping.getIdentifier(value);
            final Object disassembledKey = discriminatorMapping.getKeyPart().disassemble(identifier, session);
            valuesConsumer.consume(offset + 1, disassembledKey, discriminatorMapping.getKeyPart().getJdbcMapping());
        }
    }
    return 2;
}
Also used : EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Aggregations

EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)36 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)14 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)14 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)13 ModelPart (org.hibernate.metamodel.mapping.ModelPart)13 ArrayList (java.util.ArrayList)12 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)12 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)12 EntityPersister (org.hibernate.persister.entity.EntityPersister)11 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)10 Expression (org.hibernate.sql.ast.tree.expression.Expression)10 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)9 NavigablePath (org.hibernate.query.spi.NavigablePath)9 List (java.util.List)8 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)8 TableReference (org.hibernate.sql.ast.tree.from.TableReference)8 AbstractMap (java.util.AbstractMap)6 Collections (java.util.Collections)6 Map (java.util.Map)6 Consumer (java.util.function.Consumer)6