re-enable tests

remove org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry
re-organize some tests
This commit is contained in:
Steve Ebersole 2021-03-22 15:40:25 -05:00
parent e76da92b04
commit 1f028095cf
16 changed files with 405 additions and 486 deletions

View File

@ -18,6 +18,7 @@ import org.hibernate.boot.model.convert.spi.JpaAttributeConverterCreationContext
import org.hibernate.metamodel.model.convert.internal.JpaAttributeConverterImpl;
import org.hibernate.metamodel.model.convert.spi.JpaAttributeConverter;
import org.hibernate.resource.beans.spi.ManagedBean;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import com.fasterxml.classmate.ResolvedType;
@ -101,15 +102,23 @@ public abstract class AbstractConverterDescriptor implements ConverterDescriptor
}
@Override
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
public JpaAttributeConverter createJpaAttributeConverter(JpaAttributeConverterCreationContext context) {
final JavaTypeDescriptor<Object> converterJtd = context
.getJavaTypeDescriptorRegistry()
.getDescriptor( getAttributeConverterClass() );
final Class<?> domainJavaType = getDomainValueResolvedType().getErasedType();
final Class<?> jdbcJavaType = getRelationalValueResolvedType().getErasedType();
return new JpaAttributeConverterImpl(
createManagedBean( context ),
context.getJavaTypeDescriptorRegistry().getDescriptor( getAttributeConverterClass() ),
context.getJavaTypeDescriptorRegistry().getDescriptor( getDomainValueResolvedType().getErasedType() ),
context.getJavaTypeDescriptorRegistry().getDescriptor( getRelationalValueResolvedType().getErasedType() )
converterJtd,
domainJavaType,
jdbcJavaType,
context
);
}
protected abstract ManagedBean<? extends AttributeConverter> createManagedBean(JpaAttributeConverterCreationContext context);
protected abstract ManagedBean<? extends AttributeConverter<?, ?>> createManagedBean(JpaAttributeConverterCreationContext context);
}

View File

@ -32,9 +32,9 @@ public class ClassBasedConverterDescriptor extends AbstractConverterDescriptor {
super( converterClass, forceAutoApply, classmateContext );
}
@SuppressWarnings("unchecked")
@Override
protected ManagedBean<? extends AttributeConverter> createManagedBean(JpaAttributeConverterCreationContext context) {
return context.getManagedBeanRegistry().getBean( getAttributeConverterClass() );
protected ManagedBean<? extends AttributeConverter<?, ?>> createManagedBean(JpaAttributeConverterCreationContext context) {
//noinspection unchecked
return (ManagedBean<? extends AttributeConverter<?, ?>>) context.getManagedBeanRegistry().getBean( getAttributeConverterClass() );
}
}

View File

@ -20,16 +20,16 @@ import org.hibernate.resource.beans.spi.ProvidedInstanceManagedBeanImpl;
* @author Steve Ebersole
*/
public class InstanceBasedConverterDescriptor extends AbstractConverterDescriptor {
private final AttributeConverter converterInstance;
private final AttributeConverter<?,?> converterInstance;
public InstanceBasedConverterDescriptor(
AttributeConverter converterInstance,
AttributeConverter<?,?> converterInstance,
ClassmateContext classmateContext) {
this( converterInstance, null, classmateContext );
}
public InstanceBasedConverterDescriptor(
AttributeConverter converterInstance,
AttributeConverter<?,?> converterInstance,
Boolean forceAutoApply,
ClassmateContext classmateContext) {
super( converterInstance.getClass(), forceAutoApply, classmateContext );
@ -37,8 +37,8 @@ public class InstanceBasedConverterDescriptor extends AbstractConverterDescripto
}
@Override
@SuppressWarnings("unchecked")
protected ManagedBean<? extends AttributeConverter> createManagedBean(JpaAttributeConverterCreationContext context) {
return new ProvidedInstanceManagedBeanImpl( converterInstance );
protected ManagedBean<? extends AttributeConverter<?, ?>> createManagedBean(JpaAttributeConverterCreationContext context) {
return new ProvidedInstanceManagedBeanImpl<>( converterInstance );
}
}

View File

@ -24,6 +24,7 @@ import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.converter.AttributeConverterMutabilityPlanImpl;
import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter;
import org.hibernate.type.descriptor.java.BasicJavaDescriptor;
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
@ -114,9 +115,18 @@ public class NamedConverterResolution<J> implements BasicValue.Resolution<J> {
? explicitMutabilityPlanAccess.apply( typeConfiguration )
: null;
final MutabilityPlan mutabilityPlan = explicitMutabilityPlan != null
? explicitMutabilityPlan
: new AttributeConverterMutabilityPlanImpl( converter, true );
final MutabilityPlan mutabilityPlan;
if ( explicitMutabilityPlan != null ) {
mutabilityPlan = explicitMutabilityPlan;
}
else if ( domainJtd.getMutabilityPlan().isMutable() ) {
mutabilityPlan = new AttributeConverterMutabilityPlanImpl( converter, true );
}
else {
mutabilityPlan = ImmutableMutabilityPlan.INSTANCE;
}
return new NamedConverterResolution(
domainJtd,
relationalJtd,

View File

@ -8,9 +8,12 @@ package org.hibernate.metamodel.model.convert.internal;
import javax.persistence.AttributeConverter;
import org.hibernate.boot.model.convert.spi.JpaAttributeConverterCreationContext;
import org.hibernate.metamodel.model.convert.spi.JpaAttributeConverter;
import org.hibernate.resource.beans.spi.ManagedBean;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry;
import org.hibernate.type.descriptor.java.spi.RegistryHelper;
/**
* Standard implementation of JpaAttributeConverter
@ -19,19 +22,37 @@ import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
*/
public class JpaAttributeConverterImpl<O,R> implements JpaAttributeConverter<O,R> {
private final ManagedBean<? extends AttributeConverter<O,R>> attributeConverterBean;
private final JavaTypeDescriptor<? extends AttributeConverter<O, R>> converterJavaTypeDescriptor;
private final JavaTypeDescriptor<O> domainJavaTypeDescriptor;
private final JavaTypeDescriptor<R> relationalJavaTypeDescriptor;
private final JavaTypeDescriptor<? extends AttributeConverter<O, R>> converterJtd;
private final JavaTypeDescriptor<O> domainJtd;
private final JavaTypeDescriptor<R> jdbcJtd;
public JpaAttributeConverterImpl(
ManagedBean<? extends AttributeConverter<O, R>> attributeConverterBean,
JavaTypeDescriptor<? extends AttributeConverter<O,R>> converterJavaTypeDescriptor,
JavaTypeDescriptor<O> domainJavaTypeDescriptor,
JavaTypeDescriptor<R> relationalJavaTypeDescriptor) {
JavaTypeDescriptor<? extends AttributeConverter<O,R>> converterJtd,
JavaTypeDescriptor<O> domainJtd,
JavaTypeDescriptor<R> jdbcJtd) {
this.attributeConverterBean = attributeConverterBean;
this.converterJavaTypeDescriptor = converterJavaTypeDescriptor;
this.domainJavaTypeDescriptor = domainJavaTypeDescriptor;
this.relationalJavaTypeDescriptor = relationalJavaTypeDescriptor;
this.converterJtd = converterJtd;
this.domainJtd = domainJtd;
this.jdbcJtd = jdbcJtd;
}
public JpaAttributeConverterImpl(
ManagedBean<? extends AttributeConverter<O,R>> attributeConverterBean,
JavaTypeDescriptor<? extends AttributeConverter<O,R>> converterJtd,
Class<O> domainJavaType,
Class<R> jdbcJavaType,
JpaAttributeConverterCreationContext context) {
this.attributeConverterBean = attributeConverterBean;
this.converterJtd = converterJtd;
final JavaTypeDescriptorRegistry jtdRegistry = context.getJavaTypeDescriptorRegistry();
jdbcJtd = jtdRegistry.getDescriptor( jdbcJavaType );
domainJtd = jtdRegistry.resolveDescriptor(
domainJavaType,
() -> RegistryHelper.INSTANCE.createTypeDescriptor( domainJavaType, context.getTypeConfiguration() )
);
}
@Override
@ -51,7 +72,7 @@ public class JpaAttributeConverterImpl<O,R> implements JpaAttributeConverter<O,R
@Override
public JavaTypeDescriptor<? extends AttributeConverter<O, R>> getConverterJavaTypeDescriptor() {
return converterJavaTypeDescriptor;
return converterJtd;
}
@Override
@ -66,11 +87,11 @@ public class JpaAttributeConverterImpl<O,R> implements JpaAttributeConverter<O,R
@Override
public JavaTypeDescriptor<O> getDomainJavaTypeDescriptor() {
return domainJavaTypeDescriptor;
return domainJtd;
}
@Override
public JavaTypeDescriptor<R> getRelationalJavaTypeDescriptor() {
return relationalJavaTypeDescriptor;
return jdbcJtd;
}
}

View File

@ -61,10 +61,10 @@ public class AttributeConverterTypeAdapter<T> extends AbstractSingleColumnStanda
this.relationalJtd = relationalJtd;
this.attributeConverter = attributeConverter;
// NOTE : the way that JpaAttributeConverter get built, their "domain JTD" already
// contains the proper MutabilityPlan based on whether the `@Immuatble` is present
if ( mutabilityPlan == null ) {
this.mutabilityPlan = domainJtd.getMutabilityPlan().isMutable()
? new AttributeConverterMutabilityPlanImpl<>( attributeConverter, true )
: ImmutableMutabilityPlan.INSTANCE;
this.mutabilityPlan = (MutabilityPlan<T>) attributeConverter.getDomainJavaTypeDescriptor().getMutabilityPlan();
}
else {
this.mutabilityPlan = mutabilityPlan;

View File

@ -1,196 +0,0 @@
/*
* 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.type.descriptor.java;
import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import javax.persistence.AttributeConverter;
import org.hibernate.HibernateException;
import org.hibernate.annotations.Immutable;
import org.hibernate.annotations.Remove;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.spi.RegistryHelper;
import org.hibernate.type.spi.TypeConfiguration;
/**
* Basically a map from {@link Class} -> {@link JavaTypeDescriptor}
*
* @author Steve Ebersole
*
* @deprecated Use (5.3) Use {@link org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry} instead
*/
@Deprecated
@Remove
public class JavaTypeDescriptorRegistry implements Serializable {
private static final CoreMessageLogger log = CoreLogging.messageLogger( JavaTypeDescriptorRegistry.class );
/**
* @deprecated (5.3) Use {@link TypeConfiguration#getJavaTypeDescriptorRegistry()} instead.
*/
@Deprecated
public static final JavaTypeDescriptorRegistry INSTANCE = new JavaTypeDescriptorRegistry();
private ConcurrentHashMap<Class, JavaTypeDescriptor> descriptorsByClass = new ConcurrentHashMap<>();
public JavaTypeDescriptorRegistry() {
addDescriptorInternal( ByteTypeDescriptor.INSTANCE );
addDescriptorInternal( BooleanTypeDescriptor.INSTANCE );
addDescriptorInternal( CharacterTypeDescriptor.INSTANCE );
addDescriptorInternal( ShortTypeDescriptor.INSTANCE );
addDescriptorInternal( IntegerTypeDescriptor.INSTANCE );
addDescriptorInternal( LongTypeDescriptor.INSTANCE );
addDescriptorInternal( FloatTypeDescriptor.INSTANCE );
addDescriptorInternal( DoubleTypeDescriptor.INSTANCE );
addDescriptorInternal( BigDecimalTypeDescriptor.INSTANCE );
addDescriptorInternal( BigIntegerTypeDescriptor.INSTANCE );
addDescriptorInternal( StringTypeDescriptor.INSTANCE );
addDescriptorInternal( BlobTypeDescriptor.INSTANCE );
addDescriptorInternal( ClobTypeDescriptor.INSTANCE );
addDescriptorInternal( NClobTypeDescriptor.INSTANCE );
addDescriptorInternal( ByteArrayTypeDescriptor.INSTANCE );
addDescriptorInternal( CharacterArrayTypeDescriptor.INSTANCE );
addDescriptorInternal( PrimitiveByteArrayTypeDescriptor.INSTANCE );
addDescriptorInternal( PrimitiveCharacterArrayTypeDescriptor.INSTANCE );
addDescriptorInternal( DurationJavaDescriptor.INSTANCE );
addDescriptorInternal( InstantJavaDescriptor.INSTANCE );
addDescriptorInternal( LocalDateJavaDescriptor.INSTANCE );
addDescriptorInternal( LocalDateTimeJavaDescriptor.INSTANCE );
addDescriptorInternal( OffsetDateTimeJavaDescriptor.INSTANCE );
addDescriptorInternal( OffsetTimeJavaDescriptor.INSTANCE );
addDescriptorInternal( ZonedDateTimeJavaDescriptor.INSTANCE );
addDescriptorInternal( CalendarTypeDescriptor.INSTANCE );
descriptorsByClass.put( java.util.Date.class, JdbcTimestampTypeDescriptor.INSTANCE );
descriptorsByClass.put( java.sql.Timestamp.class, JdbcTimestampTypeDescriptor.INSTANCE );
descriptorsByClass.put( java.sql.Date.class, JdbcDateTypeDescriptor.INSTANCE );
descriptorsByClass.put( java.sql.Time.class, JdbcTimeTypeDescriptor.INSTANCE );
addDescriptorInternal( TimeZoneTypeDescriptor.INSTANCE );
addDescriptorInternal( ClassTypeDescriptor.INSTANCE );
addDescriptorInternal( CurrencyTypeDescriptor.INSTANCE );
addDescriptorInternal( LocaleTypeDescriptor.INSTANCE );
addDescriptorInternal( UrlTypeDescriptor.INSTANCE );
addDescriptorInternal( UUIDTypeDescriptor.INSTANCE );
}
private JavaTypeDescriptor addDescriptorInternal(JavaTypeDescriptor descriptor) {
return descriptorsByClass.put( descriptor.getJavaTypeClass(), descriptor );
}
/**
* Adds the given descriptor to this registry
*
* @param descriptor The descriptor to add.
*
* @deprecated (5.3) Use {@link org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry#addDescriptor(JavaTypeDescriptor)} instead.
*/
@Deprecated
public void addDescriptor(JavaTypeDescriptor descriptor) {
JavaTypeDescriptor old = addDescriptorInternal( descriptor );
if ( old != null ) {
log.debugf(
"JavaTypeDescriptorRegistry entry replaced : %s -> %s (was %s)",
descriptor.getJavaType(),
descriptor,
old
);
}
}
/**
* @deprecated (5.3) Use {@link org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry#getDescriptor(Class)} instead.
*/
@Deprecated
@SuppressWarnings("unchecked")
public <J> JavaTypeDescriptor<J> getDescriptor(Class<J> cls) {
return RegistryHelper.INSTANCE.resolveDescriptor(
descriptorsByClass,
cls,
() -> {
if ( Serializable.class.isAssignableFrom( cls ) ) {
return new SerializableTypeDescriptor( cls );
}
if ( !AttributeConverter.class.isAssignableFrom( cls ) ) {
log.debugf(
"Could not find matching JavaTypeDescriptor for requested Java class [%s]; using fallback. " +
"This means Hibernate does not know how to perform certain basic operations in relation to this Java type.",
cls.getName()
);
checkEqualsAndHashCode( cls );
}
return new FallbackJavaTypeDescriptor<>( cls );
}
);
}
@SuppressWarnings("unchecked")
private void checkEqualsAndHashCode(Class javaType) {
if ( !ReflectHelper.overridesEquals( javaType ) || !ReflectHelper.overridesHashCode( javaType ) ) {
log.unknownJavaTypeNoEqualsHashCode( javaType );
}
}
public static class FallbackJavaTypeDescriptor<T> extends AbstractClassTypeDescriptor<T> {
protected FallbackJavaTypeDescriptor(final Class<T> type) {
super( type, createMutabilityPlan( type ) );
}
@SuppressWarnings("unchecked")
private static <T> MutabilityPlan<T> createMutabilityPlan(final Class<T> type) {
if ( type.isAnnotationPresent( Immutable.class ) ) {
return ImmutableMutabilityPlan.INSTANCE;
}
// MutableMutabilityPlan is the "safest" option, but we do not necessarily know how to deepCopy etc...
return new MutableMutabilityPlan<T>() {
@Override
protected T deepCopyNotNull(T value) {
throw new HibernateException(
"Not known how to deep copy value of type: [" + type
.getName() + "]"
);
}
};
}
@Override
public String toString(T value) {
return value == null ? "<null>" : value.toString();
}
@Override
public T fromString(String string) {
throw new HibernateException(
"Not known how to convert String to given type [" + getJavaType().getTypeName() + "]"
);
}
@Override
@SuppressWarnings("unchecked")
public <X> X unwrap(T value, Class<X> type, WrapperOptions options) {
return (X) value;
}
@Override
@SuppressWarnings("unchecked")
public <X> T wrap(X value, WrapperOptions options) {
return (T) value;
}
}
}

View File

@ -44,7 +44,11 @@ public class SerializableTypeDescriptor<T extends Serializable> extends Abstract
}
public SerializableTypeDescriptor(Class<T> type) {
super( type, createMutabilityPlan( type ) );
this( type, createMutabilityPlan( type ) );
}
public SerializableTypeDescriptor(Class<T> type, MutabilityPlan<T> mutabilityPlan) {
super( type, mutabilityPlan == null ? createMutabilityPlan( type ) : mutabilityPlan );
}
@SuppressWarnings({ "unchecked" })

View File

@ -8,6 +8,7 @@ package org.hibernate.type.descriptor.java.spi;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.AbstractClassTypeDescriptor;
import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptorIndicators;
@ -22,6 +23,10 @@ public class JavaTypeDescriptorBasicAdaptor<T> extends AbstractClassTypeDescript
super( type );
}
public JavaTypeDescriptorBasicAdaptor(Class<T> type, MutabilityPlan<T> mutabilityPlan) {
super( type, mutabilityPlan );
}
@Override
public SqlTypeDescriptor getJdbcRecommendedSqlType(SqlTypeDescriptorIndicators context) {
throw new UnsupportedOperationException(

View File

@ -12,9 +12,16 @@ import java.lang.reflect.Type;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import org.hibernate.annotations.Immutable;
import org.hibernate.annotations.Mutability;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.resource.beans.spi.ManagedBean;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
import org.hibernate.type.descriptor.java.AbstractClassTypeDescriptor;
import org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor;
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.java.SerializableTypeDescriptor;
import org.hibernate.type.spi.TypeConfiguration;
import org.hibernate.type.spi.TypeConfigurationAware;
@ -35,7 +42,6 @@ public class JavaTypeDescriptorRegistry implements JavaTypeDescriptorBaseline.Ba
private final TypeConfiguration typeConfiguration;
private final ConcurrentHashMap<Type, JavaTypeDescriptor<?>> descriptorsByType = new ConcurrentHashMap<>();
@SuppressWarnings("unused")
public JavaTypeDescriptorRegistry(TypeConfiguration typeConfiguration) {
this.typeConfiguration = typeConfiguration;
JavaTypeDescriptorBaseline.prime( this );
@ -131,8 +137,6 @@ public class JavaTypeDescriptorRegistry implements JavaTypeDescriptorBaseline.Ba
return resolveDescriptor(
javaType,
() -> {
// the fallback will always be a basic type
final AbstractClassTypeDescriptor<J> fallbackDescriptor;
final Class<?> javaTypeClass;
if ( javaType instanceof Class<?> ) {
javaTypeClass = (Class<?>) javaType;
@ -142,25 +146,10 @@ public class JavaTypeDescriptorRegistry implements JavaTypeDescriptorBaseline.Ba
javaTypeClass = (Class<?>) parameterizedType.getRawType();
}
if ( javaTypeClass.isEnum() ) {
//noinspection rawtypes
fallbackDescriptor = new EnumJavaTypeDescriptor( javaTypeClass );
}
else if ( Serializable.class.isAssignableFrom( javaTypeClass ) ) {
//noinspection rawtypes
fallbackDescriptor = new SerializableTypeDescriptor( javaTypeClass );
}
else {
//noinspection rawtypes
fallbackDescriptor = new JavaTypeDescriptorBasicAdaptor( javaTypeClass );
}
// todo (6.0) : here we assume that all temporal type descriptors are registered
// ahead of time. Allow for on-the-fly temporal types? The 2 impediments for that are:
// 1) How can we recognize non-JDK date/time types?
// 2) What is the temporal precision for the types we have deemed temporal?
return fallbackDescriptor;
return RegistryHelper.INSTANCE.createTypeDescriptor(
javaTypeClass,
typeConfiguration
);
}
);
}

View File

@ -6,19 +6,26 @@
*/
package org.hibernate.type.descriptor.java.spi;
import java.util.Map;
import java.util.function.Supplier;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.function.Function;
import org.hibernate.annotations.Immutable;
import org.hibernate.annotations.Mutability;
import org.hibernate.resource.beans.spi.ManagedBean;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
import org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor;
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.jboss.logging.Logger;
import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.java.SerializableTypeDescriptor;
import org.hibernate.type.spi.TypeConfiguration;
/**
* @author Steve Ebersole
*/
public class RegistryHelper {
private static final Logger log = Logger.getLogger( RegistryHelper.class );
/**
* Singleton access
@ -29,33 +36,55 @@ public class RegistryHelper {
}
@SuppressWarnings("unchecked")
public <J> JavaTypeDescriptor<J> resolveDescriptor(
Map<Class,JavaTypeDescriptor> descriptorsByClass,
Class<J> cls,
Supplier<JavaTypeDescriptor<J>> defaultValueSupplier) {
if ( cls == null ) {
throw new IllegalArgumentException( "Class passed to locate JavaTypeDescriptor cannot be null" );
public <J> JavaTypeDescriptor<J> createTypeDescriptor(Type javaType, TypeConfiguration typeConfiguration) {
return createTypeDescriptor(
javaType,
(javaTypeClass) -> {
if ( javaTypeClass.isAnnotationPresent( Immutable.class ) ) {
return ImmutableMutabilityPlan.INSTANCE;
}
if ( javaTypeClass.isAnnotationPresent( Mutability.class ) ) {
final Mutability annotation = javaTypeClass.getAnnotation( Mutability.class );
final Class<? extends MutabilityPlan<?>> planClass = annotation.value();
final ManagedBeanRegistry managedBeanRegistry = typeConfiguration
.getServiceRegistry()
.getService( ManagedBeanRegistry.class );
final ManagedBean<? extends MutabilityPlan<?>> planBean = managedBeanRegistry.getBean( planClass );
return (MutabilityPlan<J>) planBean.getBeanInstance();
}
return ImmutableMutabilityPlan.INSTANCE;
}
);
}
@SuppressWarnings("unchecked")
public <J> JavaTypeDescriptor<J> createTypeDescriptor(
Type javaType,
Function<Class<J>,MutabilityPlan<J>> mutabilityPlanResolver) {
final Class<J> javaTypeClass;
if ( javaType instanceof Class<?> ) {
javaTypeClass = (Class<J>) javaType;
}
else {
final ParameterizedType parameterizedType = (ParameterizedType) javaType;
javaTypeClass = (Class<J>) parameterizedType.getRawType();
}
JavaTypeDescriptor<J> descriptor = descriptorsByClass.get( cls );
if ( descriptor != null ) {
return descriptor;
if ( javaTypeClass.isEnum() ) {
// enums are unequivocally immutable
//noinspection rawtypes
return new EnumJavaTypeDescriptor( javaTypeClass );
}
if ( cls.isEnum() ) {
descriptor = new EnumJavaTypeDescriptor( cls );
descriptorsByClass.put( cls, descriptor );
return descriptor;
final MutabilityPlan<J> plan = mutabilityPlanResolver.apply( javaTypeClass );
if ( Serializable.class.isAssignableFrom( javaTypeClass ) ) {
//noinspection rawtypes
return new SerializableTypeDescriptor( javaTypeClass, plan );
}
// find the first "assignable" match
for ( Map.Entry<Class, JavaTypeDescriptor> entry : descriptorsByClass.entrySet() ) {
if ( entry.getKey().isAssignableFrom( cls ) ) {
log.debugf( "Using cached JavaTypeDescriptor instance for Java class [%s]", cls.getName() );
return entry.getValue();
}
}
return defaultValueSupplier.get();
return new JavaTypeDescriptorBasicAdaptor<>( javaTypeClass, plan );
}
}

View File

@ -4,8 +4,10 @@
* 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.test.converter;
package org.hibernate.orm.test.mapping.converted;
import java.sql.Types;
import java.util.Locale;
import javax.persistence.AttributeConverter;
import javax.persistence.Cacheable;
import javax.persistence.Convert;
@ -18,18 +20,21 @@ import org.hibernate.Session;
import org.hibernate.annotations.Immutable;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.internal.MetadataBuilderImpl;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.metamodel.spi.MetamodelImplementor;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
import org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptorIndicators;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@ -39,20 +44,66 @@ import static org.hamcrest.MatcherAssert.assertThat;
*/
public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalTestCase {
private static int mutableToDatabaseCallCount;
private static int mutableToDomainCallCount;
private static int immutableToDatabaseCallCount;
private static int immutableToDomainCallCount;
private static int pseudoMutableToDatabaseCallCount;
private static int pseudoMutableToDomainCallCount;
@Test
@TestForIssue( jiraKey = "HHH-11098" )
public void testIt() {
// create data and check assertions
inTransaction(
(session) -> session.persist( new TheEntity( 1 ) )
);
// assertions based on the persist call
assertThat( mutableToDomainCallCount, is(1 ) ); // 1 instead of 0 because of the deep copy call
assertThat( mutableToDatabaseCallCount, is(2 ) ); // 2 instead of 1 because of the deep copy call
assertThat( immutableToDomainCallCount, is(0 ) ); // logical
assertThat( immutableToDatabaseCallCount, is(1 ) ); // logical
assertThat( pseudoMutableToDomainCallCount, is(0 ) ); // was 1 (like mutable) before the JavaTypeDescriptor registration
assertThat( pseudoMutableToDatabaseCallCount, is(1 ) ); // was 2 (like mutable) before the JavaTypeDescriptor registration
}
@BeforeEach
public void clearCounts() {
// in case we add additional tests
sessionFactory().getStatistics().clear();
mutableToDatabaseCallCount = 0;
mutableToDomainCallCount = 0;
immutableToDatabaseCallCount = 0;
immutableToDomainCallCount = 0;
pseudoMutableToDatabaseCallCount = 0;
pseudoMutableToDomainCallCount = 0;
}
@AfterEach
public void dropTestData() {
inTransaction(
(session) -> session.createQuery( "delete TheEntity" ).executeUpdate()
);
}
@Override
protected void configureMetadataBuilder(MetadataBuilder metadataBuilder) {
((MetadataBuilderImpl)metadataBuilder).contributeJavaTypeDescriptor(new JavaTypeDescriptorRegistry.FallbackJavaTypeDescriptor( MutableState2.class ) {
@Override
public MutabilityPlan getMutabilityPlan() {
return ImmutableMutabilityPlan.INSTANCE;
}
});
( (TypeContributions) metadataBuilder ).contributeJavaTypeDescriptor( PseudoMutableStateJavaTypeDescriptor.INSTANCE );
}
@Override
protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) {
super.configureStandardServiceRegistryBuilder( ssrb );
// to make sure we get the deepCopy calls
ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" );
ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" );
}
@ -60,48 +111,9 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT
@Override
protected void applyMetadataSources(MetadataSources sources) {
super.applyMetadataSources( sources );
sources.addAnnotatedClass( TheEntity.class );
}
@Test
@TestForIssue( jiraKey = "HHH-11098" )
public void testIt() {
// set up test data
Session session = openSession();
( (MetamodelImplementor) session.getMetamodel() ).getTypeConfiguration()
.getJavaTypeDescriptorRegistry()
.addDescriptor(
new JavaTypeDescriptorRegistry.FallbackJavaTypeDescriptor( MutableState2.class ) {
@Override
public MutabilityPlan getMutabilityPlan() {
return ImmutableMutabilityPlan.INSTANCE;
}
}
);
session.beginTransaction();
session.persist( new TheEntity(1) );
session.getTransaction().commit();
session.close();
// assertions based on the persist call
assertThat( mutableToDomainCallCount, is(1) ); // 1 instead of 0 because of the deep copy call
assertThat( mutableToDatabaseCallCount, is(2) ); // 2 instead of 1 because of the deep copy call
assertThat( immutableToDomainCallCount, is(0) ); // logical
assertThat( immutableToDatabaseCallCount, is(1) ); // logical
assertThat( immutableMutableToDomainCallCount, is(0) ); // was 1 (like mutable) before the JavaTypeDescriptor registration
assertThat( immutableMutableToDatabaseCallCount, is(1) ); // was 2 (like mutable) before the JavaTypeDescriptor registration
// clean up test data
session = openSession();
session.beginTransaction();
session.delete( session.byId( TheEntity.class ).getReference( 1 ) );
session.getTransaction().commit();
session.close();
}
@Entity( name = "TheEntity")
@Table( name = "T_ENTITY" )
@ -116,8 +128,8 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT
@Convert( converter = ImmutableConverterImpl.class )
private ImmutableState immutableState;
@Convert( converter = ImmutableMutable2ConverterImpl.class )
private MutableState2 immutableMutableState;
@Convert( converter = PseudoMutableConverterImpl.class )
private PseudoMutableState immutableMutableState;
public TheEntity() {
}
@ -127,34 +139,10 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT
this.mutableState = new MutableState( id.toString() );
this.immutableState = new ImmutableState( id.toString() );
this.immutableMutableState = new MutableState2( id.toString() );
this.immutableMutableState = new PseudoMutableState( id.toString() );
}
}
@Before
public void clearCounts() {
// in case we add additional tests
sessionFactory().getStatistics().clear();
mutableToDatabaseCallCount = 0;
mutableToDomainCallCount = 0;
immutableToDatabaseCallCount = 0;
immutableToDomainCallCount = 0;
immutableMutableToDatabaseCallCount = 0;
immutableMutableToDomainCallCount = 0;
}
private static int mutableToDatabaseCallCount;
private static int mutableToDomainCallCount;
private static int immutableToDatabaseCallCount;
private static int immutableToDomainCallCount;
private static int immutableMutableToDatabaseCallCount;
private static int immutableMutableToDomainCallCount;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Purely mutable state
@ -267,10 +255,10 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Mutable state we treat as immutable
public static class MutableState2 {
public static class PseudoMutableState {
private String state;
public MutableState2(String state) {
public PseudoMutableState(String state) {
this.state = state;
}
@ -293,7 +281,7 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT
return false;
}
MutableState2 that = (MutableState2) o;
PseudoMutableState that = (PseudoMutableState) o;
return getState() != null ? getState().equals( that.getState() ) : that.getState() == null;
@ -306,20 +294,76 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT
}
@Converter
public static class ImmutableMutable2ConverterImpl implements AttributeConverter<MutableState2,String> {
public static class PseudoMutableConverterImpl implements AttributeConverter<PseudoMutableState,String> {
@Override
public String convertToDatabaseColumn(MutableState2 attribute) {
immutableMutableToDatabaseCallCount++;
public String convertToDatabaseColumn(PseudoMutableState attribute) {
pseudoMutableToDatabaseCallCount++;
return attribute == null ? null : attribute.getState();
}
@Override
public MutableState2 convertToEntityAttribute(String dbData) {
immutableMutableToDomainCallCount++;
return new MutableState2( dbData );
public PseudoMutableState convertToEntityAttribute(String dbData) {
pseudoMutableToDomainCallCount++;
return new PseudoMutableState( dbData );
}
}
public static class PseudoMutableStateJavaTypeDescriptor implements JavaTypeDescriptor<PseudoMutableState> {
/**
* Singleton access
*/
public static final PseudoMutableStateJavaTypeDescriptor INSTANCE = new PseudoMutableStateJavaTypeDescriptor();
@Override
public Class<PseudoMutableState> getJavaTypeClass() {
return PseudoMutableState.class;
}
@Override
public MutabilityPlan<PseudoMutableState> getMutabilityPlan() {
//noinspection unchecked
return ImmutableMutabilityPlan.INSTANCE;
}
@Override
public SqlTypeDescriptor getJdbcRecommendedSqlType(SqlTypeDescriptorIndicators context) {
return context.getTypeConfiguration().getSqlTypeDescriptorRegistry().getDescriptor( Types.VARCHAR );
}
@Override
public PseudoMutableState fromString(String string) {
return string == null ? null : new PseudoMutableState( string );
}
@Override
public <X> X unwrap(PseudoMutableState value, Class<X> type, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( PseudoMutableState.class.equals( type ) ) {
return (X) value;
}
if ( String.class.equals( type ) ) {
return (X) value.state;
}
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
"Cannot convert value '%s' to type `%s`",
value.state,
type
)
);
}
@Override
public <X> PseudoMutableState wrap(X value, WrapperOptions options) {
return null;
}
}
}

View File

@ -1,4 +1,12 @@
package org.hibernate.type.descriptor.java;
/*
* 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.orm.test.type.descriptor.java;
import org.hibernate.type.descriptor.java.BooleanTypeDescriptor;
import org.junit.Test;

View File

@ -0,0 +1,110 @@
/*
* 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.orm.test.type.descriptor.java;
import java.util.Comparator;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.java.StringTypeDescriptor;
import org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptorIndicators;
import org.hibernate.type.spi.TypeConfiguration;
import org.junit.Test;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;
/**
* @author Andrea Boriero
*/
public class JavaTypeDescriptorRegistryTest {
@Test
public void testGetJavaTypeDescriptorRegistry() {
final TypeConfiguration typeConfiguration = new TypeConfiguration();
final JavaTypeDescriptorRegistry registry = new JavaTypeDescriptorRegistry( typeConfiguration );
final JavaTypeDescriptor<String> descriptor = registry.getDescriptor( String.class );
assertThat( descriptor, instanceOf( StringTypeDescriptor.class ) );
}
@Test
public void testRegisterJavaTypeDescriptorRegistry(){
final TypeConfiguration typeConfiguration = new TypeConfiguration();
final JavaTypeDescriptorRegistry registry = new JavaTypeDescriptorRegistry( typeConfiguration );
registry.addDescriptor( new CustomJavaTypeDescriptor() );
final JavaTypeDescriptor<?> descriptor = registry.getDescriptor( CustomType.class );
assertThat( descriptor, instanceOf( CustomJavaTypeDescriptor.class ) );
}
public static class CustomType {}
public static class CustomJavaTypeDescriptor implements JavaTypeDescriptor<CustomType> {
@Override
public Class<CustomType> getJavaTypeClass() {
return CustomType.class;
}
@Override
public MutabilityPlan<CustomType> getMutabilityPlan() {
return null;
}
@Override
public SqlTypeDescriptor getJdbcRecommendedSqlType(SqlTypeDescriptorIndicators context) {
return null;
}
@Override
public Comparator<CustomType> getComparator() {
return null;
}
@Override
public int extractHashCode(CustomType value) {
return 0;
}
@Override
public boolean areEqual(CustomType one, CustomType another) {
return false;
}
@Override
public String extractLoggableRepresentation(CustomType value) {
return null;
}
@Override
public String toString(CustomType value) {
return null;
}
@Override
public CustomType fromString(String string) {
return null;
}
@Override
public CustomType wrap(Object value, WrapperOptions options) {
return null;
}
@Override
public <X> X unwrap(CustomType value, Class<X> type, WrapperOptions options) {
return null;
}
}
}

View File

@ -1,13 +1,15 @@
/*
* 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>.
* 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.type.descriptor.sql;
package org.hibernate.orm.test.type.descriptor.jdbc;
import java.sql.Types;
import org.hibernate.type.descriptor.sql.JdbcTypeJavaClassMappings;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

View File

@ -1,116 +0,0 @@
/*
* 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.type;
import java.util.Comparator;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.java.StringTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptorIndicators;
import org.hibernate.type.spi.TypeConfiguration;
import org.junit.Test;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertThat;
/**
* @author Andrea Boriero
*/
public class JavaTypeDescriptorRegistryTest {
@Test
public void testGetJavaTypeDescriptorRegistry(){
TypeConfiguration typeConfiguration = new TypeConfiguration();
JavaTypeDescriptor<String> descriptor = typeConfiguration.getJavaTypeDescriptorRegistry()
.getDescriptor( String.class );
assertThat(descriptor, instanceOf(StringTypeDescriptor.class));
}
@Test
public void testRegisterJavaTypeDescriptorRegistry(){
TypeConfiguration typeConfiguration = new TypeConfiguration();
typeConfiguration.getJavaTypeDescriptorRegistry().addDescriptor( new CustomJavaTypeDescriptor() );
JavaTypeDescriptor descriptor = typeConfiguration.getJavaTypeDescriptorRegistry()
.getDescriptor( CustomType.class );
assertThat(descriptor, instanceOf(CustomJavaTypeDescriptor.class));
}
@Test
public void testAddDirectlyToJavaTypeDescriptorRegistry(){
TypeConfiguration typeConfiguration = new TypeConfiguration();
org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( new CustomJavaTypeDescriptor() );
JavaTypeDescriptor descriptor = typeConfiguration.getJavaTypeDescriptorRegistry()
.getDescriptor( CustomType.class );
assertThat(descriptor, instanceOf(CustomJavaTypeDescriptor.class));
}
public class CustomType {}
public class CustomJavaTypeDescriptor implements JavaTypeDescriptor{
@Override
public Class getJavaTypeClass() {
return CustomType.class;
}
@Override
public MutabilityPlan getMutabilityPlan() {
return null;
}
@Override
public SqlTypeDescriptor getJdbcRecommendedSqlType(SqlTypeDescriptorIndicators context) {
return null;
}
@Override
public Comparator getComparator() {
return null;
}
@Override
public int extractHashCode(Object value) {
return 0;
}
@Override
public boolean areEqual(Object one, Object another) {
return false;
}
@Override
public String extractLoggableRepresentation(Object value) {
return null;
}
@Override
public String toString(Object value) {
return null;
}
@Override
public Object fromString(String string) {
return null;
}
@Override
public Object wrap(Object value, WrapperOptions options) {
return null;
}
@Override
public Object unwrap(Object value, Class type, WrapperOptions options) {
return null;
}
}
}