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));
}
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));
}
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));
}
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]));
}
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;
}
Aggregations