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