Search in sources :

Example 1 with BasicEntityIdentifierMappingImpl

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

the class AbstractEntityPersister method generateIdentifierMapping.

protected EntityIdentifierMapping generateIdentifierMapping(Supplier<?> templateInstanceCreator, PersistentClass bootEntityDescriptor, MappingModelCreationProcess creationProcess) {
    final Type idType = getIdentifierType();
    if (idType instanceof CompositeType) {
        final CompositeType cidType = (CompositeType) idType;
        // NOTE: the term `isEmbedded` here uses Hibernate's older (pre-JPA) naming for its "non-aggregated"
        // composite-id support.  It unfortunately conflicts with the JPA usage of "embedded".  Here we normalize
        // the legacy naming to the more descriptive encapsulated versus non-encapsulated phrasing
        final boolean encapsulated = !cidType.isEmbedded();
        if (encapsulated) {
            // we have an `@EmbeddedId`
            return MappingModelCreationHelper.buildEncapsulatedCompositeIdentifierMapping(this, bootEntityDescriptor.getIdentifierProperty(), bootEntityDescriptor.getIdentifierProperty().getName(), getTableName(), rootTableKeyColumnNames, cidType, creationProcess);
        }
        // otherwise we have a non-encapsulated composite-identifier
        return generateNonEncapsulatedCompositeIdentifierMapping(creationProcess, bootEntityDescriptor);
    }
    final String columnDefinition;
    final Long length;
    final Integer precision;
    final Integer scale;
    if (bootEntityDescriptor.getIdentifier() == null) {
        columnDefinition = null;
        length = null;
        precision = null;
        scale = null;
    } else {
        Column column = bootEntityDescriptor.getIdentifier().getColumns().get(0);
        columnDefinition = column.getSqlType();
        length = column.getLength();
        precision = column.getPrecision();
        scale = column.getScale();
    }
    return new BasicEntityIdentifierMappingImpl(this, templateInstanceCreator, bootEntityDescriptor.getIdentifierProperty().getName(), getTableName(), rootTableKeyColumnNames[0], columnDefinition, length, precision, scale, (BasicType<?>) idType, creationProcess);
}
Also used : BasicType(org.hibernate.type.BasicType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) InFlightEntityMappingType(org.hibernate.metamodel.mapping.internal.InFlightEntityMappingType) AssociationType(org.hibernate.type.AssociationType) AnyType(org.hibernate.type.AnyType) CompositeType(org.hibernate.type.CompositeType) ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) JavaType(org.hibernate.type.descriptor.java.JavaType) CollectionType(org.hibernate.type.CollectionType) EntityType(org.hibernate.type.EntityType) Type(org.hibernate.type.Type) Column(org.hibernate.mapping.Column) BasicEntityIdentifierMappingImpl(org.hibernate.metamodel.mapping.internal.BasicEntityIdentifierMappingImpl) CompositeType(org.hibernate.type.CompositeType)

Example 2 with BasicEntityIdentifierMappingImpl

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

the class JoinedSubclassEntityPersister method generateIdentifierMapping.

@Override
protected EntityIdentifierMapping generateIdentifierMapping(Supplier<?> templateInstanceCreator, PersistentClass bootEntityDescriptor, MappingModelCreationProcess creationProcess) {
    final Type idType = getIdentifierType();
    if (idType instanceof CompositeType) {
        final CompositeType cidType = (CompositeType) idType;
        // NOTE: the term `isEmbedded` here uses Hibernate's older (pre-JPA) naming for its "non-aggregated"
        // composite-id support.  It unfortunately conflicts with the JPA usage of "embedded".  Here we normalize
        // the legacy naming to the more descriptive encapsulated versus non-encapsulated phrasing
        final boolean encapsulated = !cidType.isEmbedded();
        if (encapsulated) {
            // we have an `@EmbeddedId`
            return MappingModelCreationHelper.buildEncapsulatedCompositeIdentifierMapping(this, bootEntityDescriptor.getIdentifierProperty(), bootEntityDescriptor.getIdentifierProperty().getName(), getTableName(), tableKeyColumns[0], cidType, creationProcess);
        }
        // otherwise we have a non-encapsulated composite-identifier
        return generateNonEncapsulatedCompositeIdentifierMapping(creationProcess, bootEntityDescriptor);
    }
    final String columnDefinition;
    final Long length;
    final Integer precision;
    final Integer scale;
    if (bootEntityDescriptor.getIdentifier() == null) {
        columnDefinition = null;
        length = null;
        precision = null;
        scale = null;
    } else {
        Column column = bootEntityDescriptor.getIdentifier().getColumns().get(0);
        columnDefinition = column.getSqlType();
        length = column.getLength();
        precision = column.getPrecision();
        scale = column.getScale();
    }
    return new BasicEntityIdentifierMappingImpl(this, templateInstanceCreator, bootEntityDescriptor.getIdentifierProperty().getName(), getTableName(), tableKeyColumns[0][0], columnDefinition, length, precision, scale, (BasicType<?>) idType, creationProcess);
}
Also used : BasicType(org.hibernate.type.BasicType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) CompositeType(org.hibernate.type.CompositeType) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) Type(org.hibernate.type.Type) Column(org.hibernate.mapping.Column) BasicEntityIdentifierMappingImpl(org.hibernate.metamodel.mapping.internal.BasicEntityIdentifierMappingImpl) CompositeType(org.hibernate.type.CompositeType)

Example 3 with BasicEntityIdentifierMappingImpl

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

the class AccessMappingTest method testExplicitPropertyAccessAnnotationsOnProperty.

@Test
public void testExplicitPropertyAccessAnnotationsOnProperty() throws Exception {
    Configuration cfg = new Configuration();
    Class<?> classUnderTest = Course2.class;
    cfg.addAnnotatedClass(classUnderTest);
    cfg.addAnnotatedClass(Student.class);
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory(serviceRegistry);
    try {
        final EntityPersister entityPersister = factory.getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(classUnderTest.getName());
        final BasicEntityIdentifierMappingImpl identifierMapping = (BasicEntityIdentifierMappingImpl) entityPersister.getIdentifierMapping();
        assertTrue("Property access should be used.", identifierMapping.getPropertyAccess().getGetter() instanceof GetterMethodImpl);
    } finally {
        factory.close();
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) BasicEntityIdentifierMappingImpl(org.hibernate.metamodel.mapping.internal.BasicEntityIdentifierMappingImpl) Test(org.junit.Test)

Example 4 with BasicEntityIdentifierMappingImpl

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

the class AccessMappingTest method testDefaultFieldAccessIsInherited.

@Test
public void testDefaultFieldAccessIsInherited() {
    Configuration cfg = new Configuration();
    Class<?> classUnderTest = User.class;
    cfg.addAnnotatedClass(classUnderTest);
    cfg.addAnnotatedClass(Person.class);
    cfg.addAnnotatedClass(Being.class);
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory(serviceRegistry);
    try {
        final EntityPersister entityPersister = factory.getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(classUnderTest.getName());
        final BasicEntityIdentifierMappingImpl identifierMapping = (BasicEntityIdentifierMappingImpl) entityPersister.getIdentifierMapping();
        assertTrue("Field access should be used since the default access mode gets inherited", identifierMapping.getPropertyAccess().getGetter() instanceof GetterFieldImpl);
    } finally {
        factory.close();
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) GetterFieldImpl(org.hibernate.property.access.spi.GetterFieldImpl) Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) BasicEntityIdentifierMappingImpl(org.hibernate.metamodel.mapping.internal.BasicEntityIdentifierMappingImpl) Test(org.junit.Test)

Example 5 with BasicEntityIdentifierMappingImpl

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

the class AccessMappingTest method testExplicitPropertyAccessAnnotationsWithJpaStyleOverride.

@Test
public void testExplicitPropertyAccessAnnotationsWithJpaStyleOverride() {
    Configuration cfg = new Configuration();
    Class<?> classUnderTest = Course5.class;
    cfg.addAnnotatedClass(classUnderTest);
    cfg.addAnnotatedClass(Student.class);
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory(serviceRegistry);
    try {
        final EntityPersister entityPersister = factory.getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(classUnderTest.getName());
        final BasicEntityIdentifierMappingImpl identifierMapping = (BasicEntityIdentifierMappingImpl) entityPersister.getIdentifierMapping();
        assertTrue("Field access should be used.", identifierMapping.getPropertyAccess().getGetter() instanceof GetterFieldImpl);
        assertTrue("Property access should be used.", entityPersister.getAttributeMapping(0).getPropertyAccess().getGetter() instanceof GetterMethodImpl);
    } finally {
        factory.close();
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) GetterFieldImpl(org.hibernate.property.access.spi.GetterFieldImpl) Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) BasicEntityIdentifierMappingImpl(org.hibernate.metamodel.mapping.internal.BasicEntityIdentifierMappingImpl) Test(org.junit.Test)

Aggregations

BasicEntityIdentifierMappingImpl (org.hibernate.metamodel.mapping.internal.BasicEntityIdentifierMappingImpl)9 Configuration (org.hibernate.cfg.Configuration)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 Test (org.junit.Test)7 GetterFieldImpl (org.hibernate.property.access.spi.GetterFieldImpl)5 GetterMethodImpl (org.hibernate.property.access.spi.GetterMethodImpl)5 Column (org.hibernate.mapping.Column)2 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)2 BasicType (org.hibernate.type.BasicType)2 CompositeType (org.hibernate.type.CompositeType)2 Type (org.hibernate.type.Type)2 ManagedMappingType (org.hibernate.metamodel.mapping.ManagedMappingType)1 InFlightEntityMappingType (org.hibernate.metamodel.mapping.internal.InFlightEntityMappingType)1 SqlAstJoinType (org.hibernate.sql.ast.SqlAstJoinType)1 AnyType (org.hibernate.type.AnyType)1 AssociationType (org.hibernate.type.AssociationType)1 CollectionType (org.hibernate.type.CollectionType)1 EntityType (org.hibernate.type.EntityType)1 JavaType (org.hibernate.type.descriptor.java.JavaType)1