Search in sources :

Example 21 with MappingMetamodelImplementor

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

the class SingleTableEntityPersister method pruneForSubclasses.

@Override
public void pruneForSubclasses(TableGroup tableGroup, Set<String> treatedEntityNames) {
    if (!needsDiscriminator() && treatedEntityNames.isEmpty()) {
        return;
    }
    // The optimization is to simply add the discriminator filter fragment for all treated entity names
    final NamedTableReference tableReference = (NamedTableReference) tableGroup.getPrimaryTableReference();
    final InFragment frag = new InFragment();
    if (isDiscriminatorFormula()) {
        frag.setFormula("t", getDiscriminatorFormulaTemplate());
    } else {
        frag.setColumn("t", getDiscriminatorColumnName());
    }
    final MappingMetamodelImplementor mappingMetamodel = getFactory().getRuntimeMetamodels().getMappingMetamodel();
    for (String subclass : treatedEntityNames) {
        final Queryable queryable = (Queryable) mappingMetamodel.getEntityDescriptor(subclass);
        if (!queryable.isAbstract()) {
            frag.addValue(queryable.getDiscriminatorSQLValue());
        }
        if (queryable.hasSubclasses()) {
            // if the treat is an abstract class, add the concrete implementations to values if any
            Set<String> actualSubClasses = queryable.getSubclassEntityNames();
            for (String actualSubClass : actualSubClasses) {
                if (actualSubClass.equals(subclass)) {
                    continue;
                }
                final Queryable actualQueryable = (Queryable) mappingMetamodel.getEntityDescriptor(actualSubClass);
                if (!actualQueryable.hasSubclasses()) {
                    frag.addValue(actualQueryable.getDiscriminatorSQLValue());
                }
            }
        }
    }
    tableReference.setPrunedTableExpression("(select * from " + getTableName() + " t where " + frag.toFragmentString() + ")");
}
Also used : NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) InFragment(org.hibernate.sql.InFragment) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor)

Example 22 with MappingMetamodelImplementor

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

the class ConverterTest method testJPQLUpperAttributeValueBindParameterType.

@Test
public void testJPQLUpperAttributeValueBindParameterType(EntityManagerFactoryScope scope) {
    scope.inTransaction(entityManager -> {
        // tag::basic-attribute-converter-query-parameter-converter-object-example[]
        SessionFactoryImplementor sessionFactory = entityManager.getEntityManagerFactory().unwrap(SessionFactoryImplementor.class);
        final MappingMetamodelImplementor mappingMetamodel = sessionFactory.getRuntimeMetamodels().getMappingMetamodel();
        Type captionType = mappingMetamodel.getEntityDescriptor(Photo.class).getPropertyType("caption");
        Photo photo = (Photo) entityManager.createQuery("select p " + "from Photo p " + "where upper(caption) = upper(:caption) ", Photo.class).unwrap(Query.class).setParameter("caption", new Caption("Nicolae Grigorescu"), (BindableType) captionType).getSingleResult();
        // end::basic-attribute-converter-query-parameter-converter-object-example[]
        assertEquals("Dorobantul", photo.getName());
    });
}
Also used : BindableType(org.hibernate.query.BindableType) Type(org.hibernate.type.Type) Query(org.hibernate.query.Query) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Test(org.junit.jupiter.api.Test)

Example 23 with MappingMetamodelImplementor

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

the class BitSetJdbcTypeCodeTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    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(), nullValue());
    assertThat(attributeMapping.getJdbcMapping().getJdbcType().getJdbcTypeCode(), is(Types.VARBINARY));
    assertThat(attributeMapping.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass(), equalTo(BitSet.class));
    scope.inTransaction((session) -> {
        session.persist(new Product(1, BitSet.valueOf(BitSetHelper.BYTES)));
    });
    scope.inSession((session) -> {
        final Product product = session.get(Product.class, 1);
        assertThat(product.getBitSet(), equalTo(BitSet.valueOf(BitSetHelper.BYTES)));
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) BitSet(java.util.BitSet) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Test(org.junit.jupiter.api.Test)

Example 24 with MappingMetamodelImplementor

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

the class BitSetJdbcTypeTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    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(), nullValue());
    assertThat(attributeMapping.getJdbcMapping().getJdbcType().getJdbcTypeCode(), is(Types.VARBINARY));
    assertThat(attributeMapping.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass(), equalTo(BitSet.class));
    scope.inTransaction((session) -> {
        session.persist(new Product(1, BitSet.valueOf(BitSetHelper.BYTES)));
    });
    scope.inSession((session) -> {
        final Product product = session.get(Product.class, 1);
        assertThat(product.getBitSet(), equalTo(BitSet.valueOf(BitSetHelper.BYTES)));
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) BitSet(java.util.BitSet) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Test(org.junit.jupiter.api.Test)

Example 25 with MappingMetamodelImplementor

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

the class LocaleMappingTests 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(EntityWithLocale.class);
    final BasicAttributeMapping duration = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("locale");
    final JdbcMapping jdbcMapping = duration.getJdbcMapping();
    assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Locale.class));
    assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.VARCHAR)));
    scope.inTransaction((session) -> {
        session.persist(new EntityWithLocale(1, Locale.US));
    });
    scope.inTransaction((session) -> session.find(EntityWithLocale.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Locale(java.util.Locale) 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)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