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;
}
use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method getIndexInOwner.
@Override
public Object getIndexInOwner(String entity, String property, Object childEntity, Map mergeMap) {
final MappingMetamodelImplementor metamodel = session.getFactory().getRuntimeMetamodels().getMappingMetamodel();
final EntityPersister persister = metamodel.getEntityDescriptor(entity);
final CollectionPersister cp = metamodel.getCollectionDescriptor(entity + '.' + property);
// 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())) {
Object index = getIndexInParent(property, childEntity, persister, cp, parent);
if (index == null && mergeMap != null) {
final Object unMergedInstance = mergeMap.get(parent);
final Object unMergedChild = mergeMap.get(childEntity);
if (unMergedInstance != null && unMergedChild != null) {
index = getIndexInParent(property, unMergedChild, persister, cp, unMergedInstance);
LOG.debugf("A detached object being merged (corresponding to a parent in parentsByChild) has an indexed collection that [%s] the detached child being merged. ", (index != null ? "contains" : "does not contain"));
}
}
if (index != null) {
return index;
}
} else {
// remove wrong entry
removeChildParent(childEntity);
}
}
// Not found in cache, proceed
for (Entry<Object, EntityEntry> me : reentrantSafeEntityEntries()) {
final EntityEntry ee = me.getValue();
if (persister.isSubclassEntityName(ee.getEntityName())) {
final Object instance = me.getKey();
Object index = getIndexInParent(property, childEntity, persister, cp, instance);
if (index == null && mergeMap != null) {
final Object unMergedInstance = mergeMap.get(instance);
final Object unMergedChild = mergeMap.get(childEntity);
if (unMergedInstance != null && unMergedChild != null) {
index = getIndexInParent(property, unMergedChild, persister, cp, unMergedInstance);
LOG.debugf("A detached object being merged (corresponding to a managed entity) has an indexed collection that [%s] the detached child being merged. ", (index != null ? "contains" : "does not contain"));
}
}
if (index != null) {
return index;
}
}
}
return null;
}
use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.
the class BasicArrayMappingTests 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(EntityOfArrays.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));
}
{
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));
}
// and try to use the mapping
scope.inTransaction((session) -> session.persist(new EntityOfArrays(1, new Short[] { (short) 3 }, new short[] { (short) 5 })));
scope.inTransaction((session) -> session.get(EntityOfArrays.class, 1));
}
use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.
the class BigDecimalMappingTests method testMappings.
@Test
public void testMappings(SessionFactoryScope scope) {
// first, verify the type selections...
final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final EntityPersister entityDescriptor = mappingMetamodel.getEntityDescriptor(EntityOfBigDecimals.class);
final JdbcTypeRegistry jdbcTypeRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry();
{
final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapper");
assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(BigDecimal.class));
final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(BigDecimal.class));
assertThat(jdbcMapping.getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.NUMERIC)));
}
// and try to use the mapping
scope.inTransaction((session) -> session.persist(new EntityOfBigDecimals(1, BigDecimal.TEN)));
scope.inTransaction((session) -> session.get(EntityOfBigDecimals.class, 1));
}
use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.
the class BigIntegerMappingTests method testMappings.
@Test
public void testMappings(SessionFactoryScope scope) {
// first, verify the type selections...
final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final EntityPersister entityDescriptor = mappingMetamodel.getEntityDescriptor(EntityOfBigIntegers.class);
final JdbcTypeRegistry jdbcTypeRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry();
{
final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapper");
assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(BigInteger.class));
final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(BigInteger.class));
assertThat(jdbcMapping.getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.NUMERIC)));
}
// and try to use the mapping
scope.inTransaction((session) -> session.persist(new EntityOfBigIntegers(1, BigInteger.TEN)));
scope.inTransaction((session) -> session.get(EntityOfBigIntegers.class, 1));
}
Aggregations