Adapt EmbeddableInstantiator and CompositeUserType to accept a ValueAccess instead of a Supplier

This commit is contained in:
Christian Beikov 2022-02-25 16:29:31 +01:00
parent c520b48487
commit 8e6fb5636b
19 changed files with 122 additions and 72 deletions

View File

@ -9,10 +9,10 @@ package org.hibernate.userguide.mapping.basic;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Currency;
import java.util.function.Supplier;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.ValueAccess;
import org.hibernate.usertype.CompositeUserType;
/**
@ -32,9 +32,15 @@ public class MonetaryAmountUserType implements CompositeUserType<MonetaryAmount>
}
@Override
public MonetaryAmount instantiate(Supplier<Object[]> valueSupplier, SessionFactoryImplementor sessionFactory) {
final Object[] values = valueSupplier.get();
return new MonetaryAmount( (BigDecimal) values[0], (Currency) values[1] );
public MonetaryAmount instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactory) {
final BigDecimal amount = valueAccess.getValue(0, BigDecimal.class);
final Currency currency = valueAccess.getValue(1, Currency.class);
if ( amount == null && currency == null ) {
return null;
}
return new MonetaryAmount( amount, currency );
}
@Override

View File

@ -6,10 +6,9 @@
*/
package org.hibernate.metamodel.internal;
import java.util.function.Supplier;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.EmbeddableInstantiator;
import org.hibernate.metamodel.spi.ValueAccess;
import org.hibernate.usertype.CompositeUserType;
/**
@ -24,7 +23,7 @@ public class EmbeddableCompositeUserTypeInstantiator implements EmbeddableInstan
}
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
public Object instantiate(ValueAccess valuesAccess, SessionFactoryImplementor sessionFactory) {
return userType.instantiate( valuesAccess, sessionFactory );
}

View File

@ -12,6 +12,7 @@ import java.util.function.Supplier;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.mapping.Component;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.spi.ValueAccess;
/**
* Support for instantiating embeddables as dynamic-map representation
@ -31,12 +32,13 @@ public class EmbeddableInstantiatorDynamicMap
}
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
public Object instantiate(ValueAccess valuesAccess, SessionFactoryImplementor sessionFactory) {
final Map<?,?> dataMap = generateDataMap();
if ( valuesAccess != null ) {
Object[] values = valuesAccess == null ? null : valuesAccess.getValues();
if ( values != null ) {
final EmbeddableMappingType mappingType = runtimeDescriptorAccess.get();
mappingType.setValues( dataMap, valuesAccess.get() );
mappingType.setValues( dataMap, values );
}
return dataMap;

View File

@ -10,6 +10,7 @@ import java.util.function.Supplier;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.spi.ValueAccess;
import org.hibernate.type.descriptor.java.JavaType;
import static org.hibernate.bytecode.spi.ReflectionOptimizer.InstantiationOptimizer;
@ -32,10 +33,10 @@ public class EmbeddableInstantiatorPojoOptimized extends AbstractPojoInstantiato
}
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
public Object instantiate(ValueAccess valuesAccess, SessionFactoryImplementor sessionFactory) {
final Object embeddable = instantiationOptimizer.newInstance();
final EmbeddableMappingType embeddableMapping = embeddableMappingAccess.get();
embeddableMapping.setValues( embeddable, valuesAccess.get() );
embeddableMapping.setValues( embeddable, valuesAccess.getValues() );
return embeddable;
}
}

View File

@ -16,6 +16,7 @@ import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.spi.ValueAccess;
import org.hibernate.type.descriptor.java.JavaType;
/**
@ -47,7 +48,7 @@ public class EmbeddableInstantiatorPojoStandard extends AbstractPojoInstantiator
}
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
public Object instantiate(ValueAccess valuesAccess, SessionFactoryImplementor sessionFactory) {
if ( isAbstract() ) {
throw new InstantiationException(
"Cannot instantiate abstract class or interface: ", getMappedPojoClass()
@ -60,11 +61,12 @@ public class EmbeddableInstantiatorPojoStandard extends AbstractPojoInstantiator
try {
if ( constructorInjection ) {
return constructor.newInstance( valuesAccess.get() );
return constructor.newInstance( valuesAccess.getValues() );
}
Object[] values = valuesAccess == null ? null : valuesAccess.getValues();
final Object instance = constructor.newInstance();
if ( valuesAccess != null ) {
if ( values != null ) {
// At this point, createEmptyCompositesEnabled is always true.
// We can only set the property values on the compositeInstance though if there is at least one non null value.
// If the values are all null, we would normally not create a composite instance at all because no values exist.
@ -73,7 +75,7 @@ public class EmbeddableInstantiatorPojoStandard extends AbstractPojoInstantiator
// A possible alternative could be to initialize the resolved values for primitive fields to their default value,
// but that might cause unexpected outcomes for Hibernate 5 users that use createEmptyCompositesEnabled when updating.
// You can see the need for this by running EmptyCompositeEquivalentToNullTest
embeddableMappingAccess.get().setValues( instance, valuesAccess.get() );
embeddableMappingAccess.get().setValues( instance, values );
}
return instance;

View File

@ -11,6 +11,7 @@ import java.util.function.Supplier;
import org.hibernate.bytecode.spi.BasicProxyFactory;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.spi.ValueAccess;
/**
* EmbeddableInstantiator used for instantiating "proxies" of an embeddable.
@ -29,11 +30,12 @@ public class EmbeddableInstantiatorProxied implements StandardEmbeddableInstanti
}
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
public Object instantiate(ValueAccess valuesAccess, SessionFactoryImplementor sessionFactory) {
final Object proxy = factory.getProxy();
if ( valuesAccess != null ) {
Object[] values = valuesAccess == null ? null : valuesAccess.getValues();
if ( values != null ) {
final EmbeddableMappingType embeddableMapping = embeddableMappingAccess.get();
embeddableMapping.setValues( proxy, valuesAccess.get() );
embeddableMapping.setValues( proxy, values );
}
return proxy;
}

View File

@ -6,8 +6,6 @@
*/
package org.hibernate.metamodel.mapping.internal;
import java.util.function.Supplier;
import org.hibernate.bytecode.spi.ProxyFactoryFactory;
import org.hibernate.bytecode.spi.ReflectionOptimizer;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -22,6 +20,7 @@ import org.hibernate.metamodel.spi.EmbeddableInstantiator;
import org.hibernate.metamodel.spi.EmbeddableRepresentationStrategy;
import org.hibernate.metamodel.spi.EntityInstantiator;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.metamodel.spi.ValueAccess;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.type.descriptor.java.JavaType;
@ -89,10 +88,10 @@ public class VirtualIdRepresentationStrategy implements EmbeddableRepresentation
}
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
public Object instantiate(ValueAccess valuesAccess, SessionFactoryImplementor sessionFactory) {
final Object instantiated = entityInstantiator.instantiate( sessionFactory );
if ( valuesAccess != null ) {
final Object[] values = valuesAccess.get();
final Object[] values = valuesAccess.getValues();
if ( values != null ) {
virtualIdEmbeddable.setValues( instantiated, values );
}

View File

@ -26,6 +26,6 @@ public interface EmbeddableInstantiator extends Instantiator {
/**
* Create an instance of the embeddable
*/
Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory);
Object instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactory);
}

View File

@ -0,0 +1,27 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.metamodel.spi;
import org.hibernate.Incubating;
import org.hibernate.engine.spi.SessionFactoryImplementor;
/**
*
* @author Christian Beikov
*/
@Incubating
public interface ValueAccess {
Object[] getValues();
default <T> T getValue(int i, Class<T> clazz) {
return clazz.cast( getValues()[i] );
}
default Object getOwner() {
return null;
}
}

View File

@ -22,6 +22,7 @@ import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.VirtualModelPart;
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
import org.hibernate.metamodel.spi.EmbeddableRepresentationStrategy;
import org.hibernate.metamodel.spi.ValueAccess;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.query.spi.NavigablePath;
@ -43,7 +44,8 @@ import static org.hibernate.internal.util.collections.CollectionHelper.arrayList
/**
* @author Steve Ebersole
*/
public abstract class AbstractEmbeddableInitializer extends AbstractFetchParentAccess implements EmbeddableInitializer {
public abstract class AbstractEmbeddableInitializer extends AbstractFetchParentAccess implements EmbeddableInitializer,
ValueAccess {
private static final Object NULL_MARKER = new Object() {
@Override
public String toString() {
@ -205,12 +207,9 @@ public abstract class AbstractEmbeddableInitializer extends AbstractFetchParentA
// NOTE: `valuesAccess` is set to null to indicate that all values are null,
// as opposed to returning the all-null value array. the instantiator
// interprets that as the values are not known or were all null.
final Supplier<Object[]> valuesAccess = stateAllNull
? null
: () -> rowState;
final Object target = representationStrategy
.getInstantiator()
.instantiate( valuesAccess, sessionFactory);
.instantiate( this, sessionFactory);
stateInjected = true;
( (HibernateProxy) compositeInstance ).getHibernateLazyInitializer().setImplementation( target );
}
@ -321,7 +320,7 @@ public abstract class AbstractEmbeddableInitializer extends AbstractFetchParentA
final Supplier<Object[]> valuesAccess = stateAllNull == TRUE
? null
: () -> rowState;
final Object instance = representationStrategy.getInstantiator().instantiate( valuesAccess, sessionFactory );
final Object instance = representationStrategy.getInstantiator().instantiate( this, sessionFactory );
stateInjected = true;
EmbeddableLoadingLogger.INSTANCE.debugf( "Created composite instance [%s] : %s", navigablePath, instance );
@ -329,6 +328,21 @@ public abstract class AbstractEmbeddableInitializer extends AbstractFetchParentA
return instance;
}
@Override
public Object[] getValues() {
return stateAllNull ? null : rowState;
}
@Override
public <T> T getValue(int i, Class<T> clazz) {
return stateAllNull ? null : clazz.cast( rowState[i] );
}
@Override
public Object getOwner() {
return fetchParentAccess.getInitializedInstance();
}
private void handleParentInjection(RowProcessingState processingState) {
// todo (6.0) : should we initialize the composite instance if we get here and it is null (not NULL_MARKER)?

View File

@ -7,12 +7,12 @@
package org.hibernate.usertype;
import java.io.Serializable;
import java.util.function.Supplier;
import org.hibernate.HibernateException;
import org.hibernate.Incubating;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.EmbeddableInstantiator;
import org.hibernate.metamodel.spi.ValueAccess;
/**
* A <tt>UserType</tt> that may be dereferenced in a query.
@ -51,7 +51,7 @@ public interface CompositeUserType<J> extends EmbeddableInstantiator {
Object getPropertyValue(J component, int property) throws HibernateException;
@Override
J instantiate(Supplier<Object[]> values, SessionFactoryImplementor sessionFactory);
J instantiate(ValueAccess values, SessionFactoryImplementor sessionFactory);
/**
* The class that represents the embeddable mapping of the type.

View File

@ -12,16 +12,16 @@ import java.util.function.Supplier;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.EmbeddableInstantiator;
import org.hibernate.metamodel.spi.ValueAccess;
/**
* @author Steve Ebersole
*/
public class MonetaryAmountInstantiator implements EmbeddableInstantiator {
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
final Object[] values = valuesAccess.get();
final BigDecimal amount = (BigDecimal) values[0];
final Currency currency = (Currency) values[1];
public Object instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactory) {
final BigDecimal amount = valueAccess.getValue(0, BigDecimal.class);
final Currency currency = valueAccess.getValue(1, Currency.class);
if ( amount == null && currency == null ) {
return null;

View File

@ -6,21 +6,19 @@
*/
package org.hibernate.orm.test.mapping.embeddable.strategy.instantiator.embeddable;
import java.util.function.Supplier;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.EmbeddableInstantiator;
import org.hibernate.metamodel.spi.ValueAccess;
/**
* @author Steve Ebersole
*/
public class NameInstantiator implements EmbeddableInstantiator {
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
final Object[] values = valuesAccess.get();
public Object instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactory) {
// alphabetical
final String first = (String) values[0];
final String last = (String) values[1];
final String first = valueAccess.getValue( 0, String.class );
final String last = valueAccess.getValue( 1, String.class );
return new Name( first, last );
}

View File

@ -6,21 +6,19 @@
*/
package org.hibernate.orm.test.mapping.embeddable.strategy.instantiator.embedded;
import java.util.function.Supplier;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.EmbeddableInstantiator;
import org.hibernate.metamodel.spi.ValueAccess;
/**
* @author Steve Ebersole
*/
public class NameInstantiator implements EmbeddableInstantiator {
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
final Object[] values = valuesAccess.get();
public Object instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactory) {
// alphabetical
final String first = (String) values[0];
final String last = (String) values[1];
final String first = valueAccess.getValue( 0, String.class );
final String last = valueAccess.getValue( 1, String.class );
return new Name( first, last );
}

View File

@ -6,21 +6,19 @@
*/
package org.hibernate.orm.test.mapping.embeddable.strategy.instantiator.intf;
import java.util.function.Supplier;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.EmbeddableInstantiator;
import org.hibernate.metamodel.spi.ValueAccess;
/**
* @author Steve Ebersole
*/
public class NameInstantiator implements EmbeddableInstantiator {
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
final Object[] values = valuesAccess.get();
public Object instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactory) {
// alphabetical
final String first = (String) values[0];
final String last = (String) values[1];
final String first = valueAccess.getValue( 0, String.class );
final String last = valueAccess.getValue( 1, String.class );
return new NameImpl( first, last );
}

View File

@ -6,10 +6,9 @@
*/
package org.hibernate.orm.test.mapping.embeddable.strategy.instantiator.intf2;
import java.util.function.Supplier;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.EmbeddableInstantiator;
import org.hibernate.metamodel.spi.ValueAccess;
/**
* @author Steve Ebersole
@ -18,10 +17,12 @@ public class NameInstantiator implements EmbeddableInstantiator {
public static int callCount = 0;
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
public Object instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactory) {
callCount++;
final Object[] values = valuesAccess.get();
return Name.make( (String) values[0], (String) values[1] );
// alphabetical
final String first = valueAccess.getValue( 0, String.class );
final String last = valueAccess.getValue( 1, String.class );
return Name.make( first, last );
}
@Override

View File

@ -6,21 +6,19 @@
*/
package org.hibernate.orm.test.mapping.embeddable.strategy.instantiator.registered;
import java.util.function.Supplier;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.EmbeddableInstantiator;
import org.hibernate.metamodel.spi.ValueAccess;
/**
* @author Steve Ebersole
*/
public class NameInstantiator implements EmbeddableInstantiator {
@Override
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) {
final Object[] values = valuesAccess.get();
public Object instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactory) {
// alphabetical
final String first = (String) values[0];
final String last = (String) values[1];
final String first = valueAccess.getValue( 0, String.class );
final String last = valueAccess.getValue( 1, String.class );
return new Name( first, last );
}

View File

@ -9,10 +9,10 @@ package org.hibernate.orm.test.sql.hand;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Currency;
import java.util.function.Supplier;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.ValueAccess;
import org.hibernate.usertype.CompositeUserType;
/**
@ -35,9 +35,14 @@ public class MonetaryAmountUserType implements CompositeUserType<MonetaryAmount>
}
@Override
public MonetaryAmount instantiate(Supplier<Object[]> valueSupplier, SessionFactoryImplementor sessionFactory) {
final Object[] values = valueSupplier.get();
return new MonetaryAmount( (BigDecimal) values[0], (Currency) values[1] );
public MonetaryAmount instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactory) {
final BigDecimal value = valueAccess.getValue(0, BigDecimal.class);
final Currency currency = valueAccess.getValue(1, Currency.class);
if ( value == null && currency == null ) {
return null;
}
return new MonetaryAmount( value, currency );
}
@Override

View File

@ -7,10 +7,10 @@
package org.hibernate.envers.test.integration.customtype;
import java.io.Serializable;
import java.util.function.Supplier;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.spi.ValueAccess;
import org.hibernate.usertype.CompositeUserType;
import jakarta.persistence.Lob;
@ -36,8 +36,8 @@ public class ObjectUserType implements CompositeUserType<Object> {
}
@Override
public Object instantiate(Supplier<Object[]> values, SessionFactoryImplementor sessionFactory) {
return values.get()[0];
public Object instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactory) {
return valueAccess.getValue( 0, Object.class );
}
@Override