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() + ")");
}
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());
});
}
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)));
});
}
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)));
});
}
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));
}
Aggregations