Skip to content

Commit fa526f6

Browse files
janariosebersole
authored andcommitted
HHH-9475 Cannot mix @mapkey with @convert - Moved classes to reuse in test case; Added copy of type from referenced MapKey
(cherry picked from commit 80e851e)
1 parent bd20ba8 commit fa526f6

File tree

6 files changed

+468
-60
lines changed

6 files changed

+468
-60
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66
*/
77
package org.hibernate.cfg.annotations;
88

9+
import java.util.HashMap;
10+
import java.util.Iterator;
11+
import java.util.Map;
12+
import java.util.Random;
13+
import javax.persistence.AttributeOverride;
14+
import javax.persistence.AttributeOverrides;
15+
import javax.persistence.MapKeyClass;
16+
917
import org.hibernate.AnnotationException;
1018
import org.hibernate.AssertionFailure;
1119
import org.hibernate.FetchMode;
1220
import org.hibernate.MappingException;
13-
import org.hibernate.annotations.MapKeyType;
1421
import org.hibernate.annotations.common.reflection.ClassLoadingException;
1522
import org.hibernate.annotations.common.reflection.XClass;
1623
import org.hibernate.annotations.common.reflection.XProperty;
@@ -44,14 +51,6 @@
4451
import org.hibernate.mapping.Value;
4552
import org.hibernate.sql.Template;
4653

47-
import javax.persistence.AttributeOverride;
48-
import javax.persistence.AttributeOverrides;
49-
import javax.persistence.MapKeyClass;
50-
import java.util.HashMap;
51-
import java.util.Iterator;
52-
import java.util.Map;
53-
import java.util.Random;
54-
5554
/**
5655
* Implementation to bind a Map
5756
*
@@ -390,8 +389,7 @@ else if ( value instanceof SimpleValue ) {
390389
}
391390
else {
392391
targetValue = new SimpleValue( getBuildingContext().getMetadataCollector(), collection.getCollectionTable() );
393-
targetValue.setTypeName( sourceValue.getTypeName() );
394-
targetValue.setTypeParameters( sourceValue.getTypeParameters() );
392+
targetValue.copyTypeFrom( sourceValue );
395393
}
396394
Iterator columns = sourceValue.getColumnIterator();
397395
Random random = new Random();

hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,14 @@ public Properties getTypeParameters() {
525525
return typeParameters;
526526
}
527527

528+
public void copyTypeFrom( SimpleValue sourceValue ) {
529+
setTypeName( sourceValue.getTypeName() );
530+
setTypeParameters( sourceValue.getTypeParameters() );
531+
532+
type = sourceValue.type;
533+
attributeConverterDescriptor = sourceValue.attributeConverterDescriptor;
534+
}
535+
528536
@Override
529537
public String toString() {
530538
return getClass().getName() + '(' + columns.toString() + ')';
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.converter.map;
8+
9+
/**
10+
* @author Steve Ebersole
11+
* an enum-like class (converters are technically not allowed to apply to enums)
12+
*/
13+
public class ColorType {
14+
public static ColorType BLUE = new ColorType( "blue" );
15+
public static ColorType RED = new ColorType( "red" );
16+
public static ColorType YELLOW = new ColorType( "yellow" );
17+
18+
private final String color;
19+
20+
public ColorType(String color) {
21+
this.color = color;
22+
}
23+
24+
public String toExternalForm() {
25+
return color;
26+
}
27+
28+
public static ColorType fromExternalForm(String color) {
29+
if ( BLUE.color.equals( color ) ) {
30+
return BLUE;
31+
}
32+
else if ( RED.color.equals( color ) ) {
33+
return RED;
34+
}
35+
else if ( YELLOW.color.equals( color ) ) {
36+
return YELLOW;
37+
}
38+
else {
39+
throw new RuntimeException( "Unknown color : " + color );
40+
}
41+
}
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.converter.map;
8+
9+
import javax.persistence.AttributeConverter;
10+
import javax.persistence.Converter;
11+
12+
/**
13+
* @author Steve Ebersole
14+
*/
15+
@Converter(autoApply = true)
16+
public class ColorTypeConverter implements AttributeConverter<ColorType, String> {
17+
18+
@Override
19+
public String convertToDatabaseColumn(ColorType attribute) {
20+
return attribute == null ? null : attribute.toExternalForm();
21+
}
22+
23+
@Override
24+
public ColorType convertToEntityAttribute(String dbData) {
25+
return dbData == null ? null : ColorType.fromExternalForm( dbData );
26+
}
27+
}

0 commit comments

Comments
 (0)