Move annotations.enumerated tests and fix some type parameterization and enum related issues

This commit is contained in:
Christian Beikov 2021-08-04 11:50:30 +02:00
parent 38e1704f43
commit 7216823afd
16 changed files with 101 additions and 65 deletions

View File

@ -33,6 +33,30 @@ public interface ClassLoaderService extends Service, Stoppable {
*/
<T> Class<T> classForName(String className);
@SuppressWarnings("unchecked")
default <T> Class<T> classForTypeName(String className) {
switch ( className ) {
case "boolean":
return (Class<T>) boolean.class;
case "byte":
return (Class<T>) byte.class;
case "char":
return (Class<T>) char.class;
case "short":
return (Class<T>) short.class;
case "int":
return (Class<T>) int.class;
case "float":
return (Class<T>) float.class;
case "long":
return (Class<T>) long.class;
case "double":
return (Class<T>) double.class;
default:
return classForName( className );
}
}
/**
* Locate a resource by name (classpath lookup).
*

View File

@ -61,6 +61,7 @@ import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;
import org.hibernate.type.spi.TypeConfiguration;
import org.hibernate.usertype.DynamicParameterizedType;
import org.jboss.logging.Logger;
@ -103,6 +104,7 @@ public class BasicValueBinder<T> implements JdbcTypeDescriptorIndicators {
private Function<TypeConfiguration, MutabilityPlan> explicitMutabilityAccess;
private Function<TypeConfiguration, java.lang.reflect.Type> implicitJavaTypeAccess;
private XProperty xproperty;
private AccessType accessType;
private ConverterDescriptor converterDescriptor;
@ -225,6 +227,7 @@ public class BasicValueBinder<T> implements JdbcTypeDescriptorIndicators {
XClass modelPropertyTypeXClass,
String declaringClassName,
ConverterDescriptor converterDescriptor) {
this.xproperty = modelXProperty;
boolean isArray = modelXProperty.isArray();
if ( modelPropertyTypeXClass == null && !isArray ) {
// we cannot guess anything
@ -365,10 +368,7 @@ public class BasicValueBinder<T> implements JdbcTypeDescriptorIndicators {
if ( implicitJavaType.isEnum() ) {
final MapKeyEnumerated enumeratedAnn = mapAttribute.getAnnotation( MapKeyEnumerated.class );
if ( enumeratedAnn == null ) {
enumType = EnumType.ORDINAL;
}
else {
if ( enumeratedAnn != null ) {
enumType = enumeratedAnn.value();
//noinspection ConstantConditions
if ( enumType == null ) {
@ -428,10 +428,7 @@ public class BasicValueBinder<T> implements JdbcTypeDescriptorIndicators {
if ( javaType.isEnum() ) {
final Enumerated enumeratedAnn = attributeXProperty.getAnnotation( Enumerated.class );
if ( enumeratedAnn == null ) {
enumType = EnumType.ORDINAL;
}
else {
if ( enumeratedAnn != null ) {
enumType = enumeratedAnn.value();
if ( enumType == null ) {
throw new IllegalStateException(
@ -474,10 +471,7 @@ public class BasicValueBinder<T> implements JdbcTypeDescriptorIndicators {
if ( javaType.isEnum() ) {
final Enumerated enumeratedAnn = attributeDescriptor.getAnnotation( Enumerated.class );
if ( enumeratedAnn == null ) {
this.enumType = EnumType.ORDINAL;
}
else {
if ( enumeratedAnn != null ) {
this.enumType = enumeratedAnn.value();
if ( this.enumType == null ) {
throw new IllegalStateException(
@ -812,6 +806,26 @@ public class BasicValueBinder<T> implements JdbcTypeDescriptorIndicators {
basicValue.setExplicitTypeName( explicitBasicTypeName );
basicValue.setExplicitTypeParams( explicitLocalTypeParams );
// todo (6.0): Ideally we could check the type class like we did in 5.5 but that is unavailable at this point
java.lang.reflect.Type type = implicitJavaTypeAccess == null ? null : implicitJavaTypeAccess.apply( getTypeConfiguration() );
if ( xproperty != null && returnedClassName != null && ( !(type instanceof Class<?>) || !( (Class<?>) type ).isPrimitive() ) ) {
// if ( typeClass != null && DynamicParameterizedType.class.isAssignableFrom( typeClass ) ) {
final Map<String, Object> parameters = new HashMap<>();
parameters.put( DynamicParameterizedType.IS_DYNAMIC, Boolean.toString( true ) );
parameters.put( DynamicParameterizedType.RETURNED_CLASS, returnedClassName );
parameters.put( DynamicParameterizedType.IS_PRIMARY_KEY, Boolean.toString( kind == Kind.MAP_KEY ) );
parameters.put( DynamicParameterizedType.ENTITY, persistentClassName );
parameters.put( DynamicParameterizedType.XPROPERTY, xproperty );
parameters.put( DynamicParameterizedType.PROPERTY, xproperty.getName() );
if ( accessType != null ) {
parameters.put( DynamicParameterizedType.ACCESS_TYPE, accessType.getType() );
}
if ( explicitLocalTypeParams != null ) {
parameters.putAll( explicitLocalTypeParams );
}
basicValue.setTypeParameters( (Map) parameters );
}
basicValue.setJpaAttributeConverterDescriptor( converterDescriptor );

View File

@ -7,6 +7,7 @@
package org.hibernate.mapping;
import java.util.Map;
import java.util.Properties;
import java.util.function.Function;
import javax.persistence.AttributeConverter;
import javax.persistence.EnumType;
@ -48,6 +49,7 @@ import org.hibernate.type.descriptor.java.TemporalJavaTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;
import org.hibernate.type.spi.TypeConfiguration;
import org.hibernate.usertype.DynamicParameterizedType;
/**
* @author Steve Ebersole
@ -277,6 +279,12 @@ public class BasicValue extends SimpleValue implements JdbcTypeDescriptorIndicat
}
protected Resolution<?> buildResolution() {
Properties typeParameters = getTypeParameters();
if ( typeParameters != null
&& Boolean.parseBoolean( typeParameters.getProperty( DynamicParameterizedType.IS_DYNAMIC ) )
&& typeParameters.get( DynamicParameterizedType.PARAMETER_TYPE ) == null ) {
createParameterImpl();
}
if ( explicitTypeName != null ) {
return interpretExplicitlyNamedType(
explicitTypeName,
@ -286,7 +294,7 @@ public class BasicValue extends SimpleValue implements JdbcTypeDescriptorIndicat
explicitSqlTypeAccess,
explicitMutabilityPlanAccess,
getAttributeConverterDescriptor(),
explicitLocalTypeParams,
typeParameters,
this,
typeConfiguration,
getBuildingContext()
@ -363,10 +371,10 @@ public class BasicValue extends SimpleValue implements JdbcTypeDescriptorIndicat
final TypeDefinitionRegistry typeDefinitionRegistry = getBuildingContext().getTypeDefinitionRegistry();
final TypeDefinition autoAppliedTypeDef = typeDefinitionRegistry.resolveAutoApplied( (BasicJavaDescriptor<?>) jtd );
if ( autoAppliedTypeDef != null ) {
if ( autoAppliedTypeDef != null && ( !jtd.getJavaTypeClass().isEnum() || enumerationStyle == null ) ) {
log.debug( "BasicValue resolution matched auto-applied type-definition" );
return autoAppliedTypeDef.resolve(
getTypeParameters(),
typeParameters,
null,
getBuildingContext(),
this

View File

@ -818,7 +818,7 @@ public abstract class SimpleValue implements KeyValue {
this.attributeConverterDescriptor = descriptor;
}
private void createParameterImpl() {
protected void createParameterImpl() {
try {
final String[] columnNames = new String[ columns.size() ];
final Long[] columnLengths = new Long[ columns.size() ];
@ -845,7 +845,7 @@ public abstract class SimpleValue implements KeyValue {
typeParameters.put(
DynamicParameterizedType.PARAMETER_TYPE,
new ParameterTypeImpl(
classLoaderService.classForName(
classLoaderService.classForTypeName(
typeParameters.getProperty( DynamicParameterizedType.RETURNED_CLASS )
),
annotations,

View File

@ -352,14 +352,6 @@ public class MappingModelCreationHelper {
final ValueGeneration valueGeneration = bootProperty.getValueGenerationStrategy();
if ( valueConverter != null ) {
if ( isAttrFormula ) {
throw new MappingException( String.format(
"Value converter should not be set for column [%s] annotated with @Formula [%s]",
attrName,
attrColumnName
) );
}
// we want to "decompose" the "type" into its various pieces as expected by the mapping
assert valueConverter.getRelationalJavaDescriptor() == resolution.getRelationalJavaDescriptor();
@ -386,7 +378,7 @@ public class MappingModelCreationHelper {
fetchStyle,
tableExpression,
attrColumnName,
false,
isAttrFormula,
null,
null,
valueConverter,

View File

@ -4,7 +4,7 @@
* 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.annotations.enumerated;
package org.hibernate.orm.test.annotations.enumerated;
import java.util.HashSet;
import java.util.Set;
@ -23,11 +23,11 @@ import org.hibernate.annotations.Formula;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.test.annotations.enumerated.custom_types.LastNumberType;
import org.hibernate.test.annotations.enumerated.enums.Common;
import org.hibernate.test.annotations.enumerated.enums.FirstLetter;
import org.hibernate.test.annotations.enumerated.enums.LastNumber;
import org.hibernate.test.annotations.enumerated.enums.Trimmed;
import org.hibernate.orm.test.annotations.enumerated.custom_types.LastNumberType;
import org.hibernate.orm.test.annotations.enumerated.enums.Common;
import org.hibernate.orm.test.annotations.enumerated.enums.FirstLetter;
import org.hibernate.orm.test.annotations.enumerated.enums.LastNumber;
import org.hibernate.orm.test.annotations.enumerated.enums.Trimmed;
/**
* @author Janario Oliveira
@ -43,7 +43,7 @@ public class EntityEnum {
private Common ordinal;
@Enumerated(EnumType.STRING)
private Common string;
@Type(type = "org.hibernate.test.annotations.enumerated.custom_types.FirstLetterType")
@Type(type = "org.hibernate.orm.test.annotations.enumerated.custom_types.FirstLetterType")
private FirstLetter firstLetter;
private LastNumber lastNumber;
@Enumerated(EnumType.STRING)

View File

@ -4,7 +4,7 @@
* 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.annotations.enumerated;
package org.hibernate.orm.test.annotations.enumerated;
import java.sql.Types;
import javax.persistence.Entity;
@ -75,7 +75,7 @@ public class EnumeratedSmokeTest extends BaseUnitTestCase {
final org.hibernate.type.EnumType hibernateMappingEnumType = (org.hibernate.type.EnumType) customType.getUserType();
assertThat( hibernateMappingEnumType.isOrdinal(), is(expectedJpaEnumType==EnumType.ORDINAL) );
assertThat( hibernateMappingEnumType.sqlTypes().length, is(1) );
assertThat( hibernateMappingEnumType.sqlTypes()[0], is(expectedJpaEnumType==EnumType.ORDINAL? Types.INTEGER:Types.VARCHAR) );
assertThat( hibernateMappingEnumType.sqlTypes()[0], is(expectedJpaEnumType==EnumType.ORDINAL? Types.TINYINT:Types.VARCHAR) );
}
@Entity

View File

@ -4,7 +4,7 @@
* 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.annotations.enumerated;
package org.hibernate.orm.test.annotations.enumerated;
import java.sql.Connection;
import java.sql.SQLException;
@ -12,13 +12,12 @@ import java.sql.Statement;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;
import org.hibernate.Session;
import org.hibernate.dialect.AbstractHANADialect;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.query.Query;
@ -28,12 +27,12 @@ import org.hibernate.type.Type;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.test.annotations.enumerated.custom_types.FirstLetterType;
import org.hibernate.test.annotations.enumerated.custom_types.LastNumberType;
import org.hibernate.test.annotations.enumerated.enums.Common;
import org.hibernate.test.annotations.enumerated.enums.FirstLetter;
import org.hibernate.test.annotations.enumerated.enums.LastNumber;
import org.hibernate.test.annotations.enumerated.enums.Trimmed;
import org.hibernate.orm.test.annotations.enumerated.custom_types.FirstLetterType;
import org.hibernate.orm.test.annotations.enumerated.custom_types.LastNumberType;
import org.hibernate.orm.test.annotations.enumerated.enums.Common;
import org.hibernate.orm.test.annotations.enumerated.enums.FirstLetter;
import org.hibernate.orm.test.annotations.enumerated.enums.LastNumber;
import org.hibernate.orm.test.annotations.enumerated.enums.Trimmed;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@ -249,8 +248,7 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<EntityEnum> criteria = criteriaBuilder.createQuery( EntityEnum.class );
Root<EntityEnum> root = criteria.from( EntityEnum.class );
Join<Object, Object> ordinal = root.join( "ordinal", JoinType.INNER );
criteria.where( criteriaBuilder.equal( ordinal, Common.A1 ) );
criteria.where( criteriaBuilder.equal( root.get( "ordinal" ), Common.A1 ) );
entityEnum = session.createQuery( criteria ).uniqueResult();
// entityEnum = (EntityEnum) session.createCriteria( EntityEnum.class )
@ -385,7 +383,7 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
@TestForIssue(jiraKey = "HHH-4699")
@SkipForDialect(value = { Oracle8iDialect.class, AbstractHANADialect.class }, jiraKey = "HHH-8516",
@SkipForDialect(value = { OracleDialect.class, AbstractHANADialect.class }, jiraKey = "HHH-8516",
comment = "HHH-4699 was specifically for using a CHAR, but Oracle/HANA do not handle the 2nd query correctly without VARCHAR. ")
public void testTrimmedEnumChar() throws SQLException {
// use native SQL to insert, forcing whitespace to occur

View File

@ -4,7 +4,7 @@
* 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.annotations.enumerated.custom_mapkey;
package org.hibernate.orm.test.annotations.enumerated.custom_mapkey;
import java.util.HashMap;
import java.util.Map;
@ -20,11 +20,11 @@ import javax.persistence.MapKeyEnumerated;
import org.hibernate.annotations.MapKeyType;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.orm.test.annotations.enumerated.custom_types.LastNumberType;
import org.hibernate.test.annotations.enumerated.custom_types.LastNumberType;
import org.hibernate.test.annotations.enumerated.enums.Common;
import org.hibernate.test.annotations.enumerated.enums.FirstLetter;
import org.hibernate.test.annotations.enumerated.enums.LastNumber;
import org.hibernate.orm.test.annotations.enumerated.enums.Common;
import org.hibernate.orm.test.annotations.enumerated.enums.FirstLetter;
import org.hibernate.orm.test.annotations.enumerated.enums.LastNumber;
/**
* @author Janario Oliveira
@ -42,7 +42,7 @@ public class EntityMapEnum {
@MapKeyEnumerated(EnumType.STRING)
Map<Common, String> stringMap = new HashMap<Common, String>();
@ElementCollection
@MapKeyType(@Type(type = "org.hibernate.test.annotations.enumerated.custom_types.FirstLetterType"))
@MapKeyType(@Type(type = "org.hibernate.orm.test.annotations.enumerated.custom_types.FirstLetterType"))
Map<FirstLetter, String> firstLetterMap = new HashMap<FirstLetter, String>();
@ElementCollection
Map<LastNumber, String> lastNumberMap = new HashMap<LastNumber, String>();

View File

@ -4,7 +4,7 @@
* 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.annotations.enumerated.custom_mapkey;
package org.hibernate.orm.test.annotations.enumerated.custom_mapkey;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
@ -22,11 +22,11 @@ import org.hibernate.type.EnumType;
import org.hibernate.type.Type;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.test.annotations.enumerated.custom_types.FirstLetterType;
import org.hibernate.test.annotations.enumerated.custom_types.LastNumberType;
import org.hibernate.test.annotations.enumerated.enums.Common;
import org.hibernate.test.annotations.enumerated.enums.FirstLetter;
import org.hibernate.test.annotations.enumerated.enums.LastNumber;
import org.hibernate.orm.test.annotations.enumerated.custom_types.FirstLetterType;
import org.hibernate.orm.test.annotations.enumerated.custom_types.LastNumberType;
import org.hibernate.orm.test.annotations.enumerated.enums.Common;
import org.hibernate.orm.test.annotations.enumerated.enums.FirstLetter;
import org.hibernate.orm.test.annotations.enumerated.enums.LastNumber;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

View File

@ -4,7 +4,7 @@
* 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.annotations.enumerated.custom_types;
package org.hibernate.orm.test.annotations.enumerated.custom_types;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

View File

@ -4,7 +4,7 @@
* 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.annotations.enumerated.custom_types;
package org.hibernate.orm.test.annotations.enumerated.custom_types;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

View File

@ -4,7 +4,7 @@
* 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.annotations.enumerated.enums;
package org.hibernate.orm.test.annotations.enumerated.enums;
/**
* @author Janario Oliveira

View File

@ -4,7 +4,7 @@
* 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.annotations.enumerated.enums;
package org.hibernate.orm.test.annotations.enumerated.enums;
/**
* @author Janario Oliveira

View File

@ -4,7 +4,7 @@
* 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.annotations.enumerated.enums;
package org.hibernate.orm.test.annotations.enumerated.enums;
/**
* @author Janario Oliveira

View File

@ -4,7 +4,7 @@
* 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.annotations.enumerated.enums;
package org.hibernate.orm.test.annotations.enumerated.enums;
/**
* @author Janario Oliveira