HHH-15430 Allow use of @Enumerated on basic collection

This commit is contained in:
Christian Beikov 2022-08-03 14:21:04 +02:00
parent 23a487628a
commit 446bcb5a00
2 changed files with 16 additions and 2 deletions

View File

@ -9,6 +9,7 @@ package org.hibernate.cfg.annotations;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -727,7 +728,7 @@ public class BasicValueBinder implements JdbcTypeIndicators {
final Enumerated enumeratedAnn = attributeDescriptor.getAnnotation( Enumerated.class ); final Enumerated enumeratedAnn = attributeDescriptor.getAnnotation( Enumerated.class );
if ( enumeratedAnn != null ) { if ( enumeratedAnn != null ) {
this.enumType = enumeratedAnn.value(); this.enumType = enumeratedAnn.value();
if ( javaTypeClass.isEnum() || javaTypeClass.isArray() && javaTypeClass.getComponentType().isEnum() ) { if ( canUseEnumerated( javaType, javaTypeClass ) ) {
if ( this.enumType == null ) { if ( this.enumType == null ) {
throw new IllegalStateException( throw new IllegalStateException(
"jakarta.persistence.EnumType was null on @jakarta.persistence.Enumerated " + "jakarta.persistence.EnumType was null on @jakarta.persistence.Enumerated " +
@ -754,6 +755,19 @@ public class BasicValueBinder implements JdbcTypeIndicators {
normalSupplementalDetails( attributeDescriptor); normalSupplementalDetails( attributeDescriptor);
} }
private boolean canUseEnumerated(java.lang.reflect.Type javaType, Class<Object> javaTypeClass) {
if ( javaTypeClass.isEnum() || javaTypeClass.isArray() && javaTypeClass.getComponentType().isEnum() ) {
return true;
}
if ( Collection.class.isAssignableFrom( javaTypeClass ) ) {
final java.lang.reflect.Type[] typeArguments = ( (ParameterizedType) javaType ).getActualTypeArguments();
if ( typeArguments.length != 0 ) {
return ReflectHelper.getClass( typeArguments[0] ).isEnum();
}
}
return false;
}
private void prepareAnyDiscriminator(XProperty modelXProperty) { private void prepareAnyDiscriminator(XProperty modelXProperty) {
final AnyDiscriminator anyDiscriminatorAnn = findAnnotation( modelXProperty, AnyDiscriminator.class ); final AnyDiscriminator anyDiscriminatorAnn = findAnnotation( modelXProperty, AnyDiscriminator.class );

View File

@ -151,7 +151,7 @@ public class EnumSetTest extends BaseNonConfigCoreFunctionalTestCase {
@Id @Id
private Long id; private Long id;
// @Enumerated(EnumType.ORDINAL) @Enumerated(EnumType.ORDINAL)
@Column( name = "the_set" ) @Column( name = "the_set" )
private Set<MyEnum> theSet; private Set<MyEnum> theSet;