Skip to content

Commit 7ccbd46

Browse files
janariosebersole
authored andcommitted
HHH-9534 - Exception with custom EnumType and Map relation
1 parent 3192e1d commit 7ccbd46

File tree

12 files changed

+465
-48
lines changed

12 files changed

+465
-48
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/annotations/MapBinder.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,19 +271,12 @@ else if ( owner.getIdentifierMapper() != null && owner.getIdentifierMapper().get
271271
//do not call setType as it extract the type from @Type
272272
//the algorithm generally does not apply for map key anyway
273273
elementBinder.setKey(true);
274-
MapKeyType mapKeyTypeAnnotation = property.getAnnotation( MapKeyType.class );
275-
if ( mapKeyTypeAnnotation != null
276-
&& !BinderHelper.isEmptyAnnotationValue( mapKeyTypeAnnotation.value() .type() ) ) {
277-
elementBinder.setExplicitType( mapKeyTypeAnnotation.value() );
278-
}
279-
else {
280-
elementBinder.setType(
281-
property,
282-
keyXClass,
283-
this.collection.getOwnerEntityName(),
284-
holder.keyElementAttributeConverterDefinition( keyXClass )
285-
);
286-
}
274+
elementBinder.setType(
275+
property,
276+
keyXClass,
277+
this.collection.getOwnerEntityName(),
278+
holder.keyElementAttributeConverterDefinition( keyXClass )
279+
);
287280
elementBinder.setPersistentClassName( propertyHolder.getEntityName() );
288281
elementBinder.setAccessType( accessType );
289282
mapValue.setIndex( elementBinder.make() );

hibernate-core/src/main/java/org/hibernate/cfg/annotations/SimpleValueBinder.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.hibernate.AnnotationException;
2222
import org.hibernate.AssertionFailure;
2323
import org.hibernate.MappingException;
24+
import org.hibernate.annotations.MapKeyType;
2425
import org.hibernate.annotations.Nationalized;
2526
import org.hibernate.annotations.Parameter;
2627
import org.hibernate.annotations.Type;
@@ -147,7 +148,18 @@ public void setType(XProperty property, XClass returnedClass, String declaringCl
147148
isNationalized = property.isAnnotationPresent( Nationalized.class )
148149
|| buildingContext.getBuildingOptions().useNationalizedCharacterData();
149150

150-
Type annType = property.getAnnotation( Type.class );
151+
Type annType = null;
152+
if ( (!key && property.isAnnotationPresent( Type.class ))
153+
|| (key && property.isAnnotationPresent( MapKeyType.class )) ) {
154+
if ( key ) {
155+
MapKeyType ann = property.getAnnotation( MapKeyType.class );
156+
annType = ann.value();
157+
}
158+
else {
159+
annType = property.getAnnotation( Type.class );
160+
}
161+
}
162+
151163
if ( annType != null ) {
152164
setExplicitType( annType );
153165
type = explicitType;

hibernate-core/src/test/java/org/hibernate/test/annotations/enumerated/EntityEnum.java

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,29 @@
1919
import javax.persistence.JoinColumn;
2020
import javax.persistence.JoinTable;
2121

22-
import org.hibernate.annotations.Formula;
23-
import org.hibernate.annotations.Type;
24-
import org.hibernate.annotations.TypeDef;
25-
import org.hibernate.annotations.TypeDefs;
22+
import org.hibernate.annotations.*;
23+
24+
import org.hibernate.test.annotations.enumerated.custom_types.LastNumberType;
25+
import org.hibernate.test.annotations.enumerated.enums.Common;
26+
import org.hibernate.test.annotations.enumerated.enums.FirstLetter;
27+
import org.hibernate.test.annotations.enumerated.enums.LastNumber;
28+
import org.hibernate.test.annotations.enumerated.enums.Trimmed;
2629

2730
/**
2831
* @author Janario Oliveira
2932
* @author Brett Meyer
3033
*/
3134
@Entity
32-
@TypeDefs({ @TypeDef(typeClass = LastNumberType.class, defaultForType = EntityEnum.LastNumber.class) })
35+
@TypeDefs({ @TypeDef(typeClass = LastNumberType.class, defaultForType = LastNumber.class) })
3336
public class EntityEnum {
3437

35-
enum Common {
36-
37-
A1, A2, B1, B2
38-
}
39-
40-
enum FirstLetter {
41-
42-
A_LETTER, B_LETTER, C_LETTER
43-
}
44-
45-
enum LastNumber {
46-
47-
NUMBER_1, NUMBER_2, NUMBER_3
48-
}
49-
50-
enum Trimmed {
51-
52-
A, B, C
53-
}
54-
5538
@Id
5639
@GeneratedValue
5740
private long id;
5841
private Common ordinal;
5942
@Enumerated(EnumType.STRING)
6043
private Common string;
61-
@Type(type = "org.hibernate.test.annotations.enumerated.FirstLetterType")
44+
@Type(type = "org.hibernate.test.annotations.enumerated.custom_types.FirstLetterType")
6245
private FirstLetter firstLetter;
6346
private LastNumber lastNumber;
6447
@Enumerated(EnumType.STRING)

hibernate-core/src/test/java/org/hibernate/test/annotations/enumerated/EnumeratedTypeTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@
2222
import org.hibernate.type.EnumType;
2323
import org.hibernate.type.Type;
2424

25-
import org.junit.Test;
26-
2725
import org.hibernate.testing.SkipForDialect;
2826
import org.hibernate.testing.TestForIssue;
2927
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
30-
import org.hibernate.test.annotations.enumerated.EntityEnum.Common;
31-
import org.hibernate.test.annotations.enumerated.EntityEnum.FirstLetter;
32-
import org.hibernate.test.annotations.enumerated.EntityEnum.LastNumber;
33-
import org.hibernate.test.annotations.enumerated.EntityEnum.Trimmed;
28+
import org.hibernate.test.annotations.enumerated.custom_types.FirstLetterType;
29+
import org.hibernate.test.annotations.enumerated.custom_types.LastNumberType;
30+
import org.hibernate.test.annotations.enumerated.enums.Common;
31+
import org.hibernate.test.annotations.enumerated.enums.FirstLetter;
32+
import org.hibernate.test.annotations.enumerated.enums.LastNumber;
33+
import org.hibernate.test.annotations.enumerated.enums.Trimmed;
34+
import org.junit.Test;
3435

3536
import static org.junit.Assert.assertEquals;
3637

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.annotations.enumerated.custom_mapkey;
8+
9+
import javax.persistence.ElementCollection;
10+
import javax.persistence.Entity;
11+
import javax.persistence.EnumType;
12+
import javax.persistence.GeneratedValue;
13+
import javax.persistence.Id;
14+
import javax.persistence.MapKeyEnumerated;
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
import org.hibernate.annotations.MapKeyType;
19+
import org.hibernate.annotations.Type;
20+
import org.hibernate.annotations.TypeDef;
21+
import org.hibernate.annotations.TypeDefs;
22+
23+
import org.hibernate.test.annotations.enumerated.custom_types.LastNumberType;
24+
import org.hibernate.test.annotations.enumerated.enums.Common;
25+
import org.hibernate.test.annotations.enumerated.enums.FirstLetter;
26+
import org.hibernate.test.annotations.enumerated.enums.LastNumber;
27+
28+
/**
29+
* @author Janario Oliveira
30+
*/
31+
@Entity
32+
@TypeDefs({@TypeDef(typeClass = LastNumberType.class, defaultForType = LastNumber.class)})
33+
public class EntityMapEnum {
34+
@Id
35+
@GeneratedValue
36+
int id;
37+
38+
@ElementCollection
39+
Map<Common, String> ordinalMap = new HashMap<Common, String>();
40+
@ElementCollection
41+
@MapKeyEnumerated(EnumType.STRING)
42+
Map<Common, String> stringMap = new HashMap<Common, String>();
43+
@ElementCollection
44+
@MapKeyType(@Type(type = "org.hibernate.test.annotations.enumerated.custom_types.FirstLetterType"))
45+
Map<FirstLetter, String> firstLetterMap = new HashMap<FirstLetter, String>();
46+
@ElementCollection
47+
Map<LastNumber, String> lastNumberMap = new HashMap<LastNumber, String>();
48+
@MapKeyEnumerated(EnumType.STRING)
49+
@ElementCollection
50+
Map<LastNumber, String> explicitOverridingImplicitMap = new HashMap<LastNumber, String>();
51+
}

0 commit comments

Comments
 (0)