Search in sources :

Example 1 with EmbeddableInstantiator

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

the class ComponentType method replace.

@Override
public Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map<Object, Object> copyCache) {
    if (original == null) {
        return null;
    }
    if (compositeUserType != null) {
        return compositeUserType.replace(original, target, owner);
    }
    // if ( original == target ) return target;
    final Object[] originalValues = getPropertyValues(original);
    final Object[] resultValues;
    if (target == null) {
        resultValues = new Object[originalValues.length];
    } else {
        resultValues = getPropertyValues(target);
    }
    final Object[] replacedValues = TypeHelper.replace(originalValues, resultValues, propertyTypes, session, owner, copyCache);
    if (target == null) {
        final EmbeddableInstantiator instantiator = mappingModelPart.getEmbeddableTypeDescriptor().getRepresentationStrategy().getInstantiator();
        return instantiator.instantiate(() -> replacedValues, session.getSessionFactory());
    } else {
        setPropertyValues(target, replacedValues);
        return target;
    }
}
Also used : EmbeddableInstantiator(org.hibernate.metamodel.spi.EmbeddableInstantiator)

Example 2 with EmbeddableInstantiator

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

the class ComponentType method deepCopy.

@Override
public Object deepCopy(Object component, SessionFactoryImplementor factory) {
    if (component == null) {
        return null;
    }
    if (compositeUserType != null) {
        return compositeUserType.deepCopy(component);
    }
    final Object[] values = getPropertyValues(component);
    for (int i = 0; i < propertySpan; i++) {
        values[i] = propertyTypes[i].deepCopy(values[i], factory);
    }
    final EmbeddableInstantiator instantiator = mappingModelPart.getEmbeddableTypeDescriptor().getRepresentationStrategy().getInstantiator();
    Object result = instantiator.instantiate(() -> values, factory);
    // not absolutely necessary, but helps for some
    // equals()/hashCode() implementations
    final PropertyAccess parentAccess = mappingModelPart().getParentInjectionAttributePropertyAccess();
    if (parentAccess != null) {
        parentAccess.getSetter().set(result, parentAccess.getGetter().get(component));
    }
    return result;
}
Also used : EmbeddableInstantiator(org.hibernate.metamodel.spi.EmbeddableInstantiator) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess)

Example 3 with EmbeddableInstantiator

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

the class ComponentType method replace.

@Override
public Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map<Object, Object> copyCache, ForeignKeyDirection foreignKeyDirection) {
    if (original == null) {
        return null;
    }
    if (compositeUserType != null) {
        return compositeUserType.replace(original, target, owner);
    }
    // if ( original == target ) return target;
    final Object[] originalValues = getPropertyValues(original);
    final Object[] resultValues;
    if (target == null) {
        resultValues = new Object[originalValues.length];
    } else {
        resultValues = getPropertyValues(target);
    }
    final Object[] replacedValues = TypeHelper.replace(originalValues, resultValues, propertyTypes, session, owner, copyCache, foreignKeyDirection);
    if (target == null) {
        final EmbeddableInstantiator instantiator = mappingModelPart.getEmbeddableTypeDescriptor().getRepresentationStrategy().getInstantiator();
        return instantiator.instantiate(() -> replacedValues, session.getSessionFactory());
    } else {
        setPropertyValues(target, replacedValues);
        return target;
    }
}
Also used : EmbeddableInstantiator(org.hibernate.metamodel.spi.EmbeddableInstantiator)

Example 4 with EmbeddableInstantiator

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

the class ManagedTypeRepresentationResolverStandard method resolveStrategy.

@Override
public EmbeddableRepresentationStrategy resolveStrategy(Component bootDescriptor, Supplier<EmbeddableMappingType> runtimeDescriptorAccess, RuntimeModelCreationContext creationContext) {
    // RepresentationMode representation = bootDescriptor.getExplicitRepresentationMode();
    RepresentationMode representation = null;
    if (representation == null) {
        if (bootDescriptor.getComponentClassName() == null) {
            representation = RepresentationMode.MAP;
        } else {
            representation = RepresentationMode.POJO;
        }
    }
    final CompositeUserType<Object> compositeUserType;
    if (bootDescriptor.getTypeName() != null) {
        compositeUserType = (CompositeUserType<Object>) creationContext.getBootstrapContext().getServiceRegistry().getService(ManagedBeanRegistry.class).getBean(creationContext.getBootstrapContext().getClassLoaderAccess().classForName(bootDescriptor.getTypeName())).getBeanInstance();
    } else {
        compositeUserType = null;
    }
    final EmbeddableInstantiator customInstantiator;
    if (bootDescriptor.getCustomInstantiator() != null) {
        final Class<? extends EmbeddableInstantiator> customInstantiatorImpl = bootDescriptor.getCustomInstantiator();
        customInstantiator = creationContext.getBootstrapContext().getServiceRegistry().getService(ManagedBeanRegistry.class).getBean(customInstantiatorImpl).getBeanInstance();
    } else if (compositeUserType != null) {
        customInstantiator = new EmbeddableCompositeUserTypeInstantiator(compositeUserType);
    } else {
        customInstantiator = null;
    }
    if (representation == RepresentationMode.MAP) {
        return new EmbeddableRepresentationStrategyMap(bootDescriptor, runtimeDescriptorAccess, customInstantiator, creationContext);
    } else {
        // StandardPojoRepresentationStrategy
        return new EmbeddableRepresentationStrategyPojo(bootDescriptor, runtimeDescriptorAccess, customInstantiator, compositeUserType, creationContext);
    }
}
Also used : ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry) RepresentationMode(org.hibernate.metamodel.RepresentationMode) EmbeddableInstantiator(org.hibernate.metamodel.spi.EmbeddableInstantiator)

Example 5 with EmbeddableInstantiator

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

the class ComponentBasicProxyTest method testOnlyOneProxyClassGenerated.

@Test
@TestForIssue(jiraKey = "HHH-12791")
public void testOnlyOneProxyClassGenerated(DomainModelScope domainModelScope, SessionFactoryScope sfScope) {
    final SessionFactoryImplementor sessionFactory = sfScope.getSessionFactory();
    final PersistentClass personDescriptor = domainModelScope.getDomainModel().getEntityBinding(Person.class.getName());
    final CompositeTypeImplementor componentType = (CompositeTypeImplementor) personDescriptor.getIdentifierMapper().getType();
    final EmbeddableValuedModelPart embedded = componentType.getMappingModelPart();
    final EmbeddableInstantiator instantiator = embedded.getEmbeddableTypeDescriptor().getRepresentationStrategy().getInstantiator();
    final Object instance1 = instantiator.instantiate(null, sessionFactory);
    final Object instance2 = instantiator.instantiate(null, sessionFactory);
    assertThat(instance1.getClass()).isEqualTo(instance2.getClass());
}
Also used : CompositeTypeImplementor(org.hibernate.type.spi.CompositeTypeImplementor) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) EmbeddableInstantiator(org.hibernate.metamodel.spi.EmbeddableInstantiator) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

EmbeddableInstantiator (org.hibernate.metamodel.spi.EmbeddableInstantiator)6 ManagedBeanRegistry (org.hibernate.resource.beans.spi.ManagedBeanRegistry)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 ComponentAuditingData (org.hibernate.envers.configuration.internal.metadata.reader.ComponentAuditingData)1 PropertyAuditingData (org.hibernate.envers.configuration.internal.metadata.reader.PropertyAuditingData)1 EntityConfiguration (org.hibernate.envers.internal.entities.EntityConfiguration)1 CompositeMapperBuilder (org.hibernate.envers.internal.entities.mapper.CompositeMapperBuilder)1 Component (org.hibernate.mapping.Component)1 PersistentClass (org.hibernate.mapping.PersistentClass)1 Property (org.hibernate.mapping.Property)1 RepresentationMode (org.hibernate.metamodel.RepresentationMode)1 EmbeddableCompositeUserTypeInstantiator (org.hibernate.metamodel.internal.EmbeddableCompositeUserTypeInstantiator)1 EmbeddableValuedModelPart (org.hibernate.metamodel.mapping.EmbeddableValuedModelPart)1 PropertyAccess (org.hibernate.property.access.spi.PropertyAccess)1 TestForIssue (org.hibernate.testing.TestForIssue)1 CompositeTypeImplementor (org.hibernate.type.spi.CompositeTypeImplementor)1 CompositeUserType (org.hibernate.usertype.CompositeUserType)1 Test (org.junit.jupiter.api.Test)1