6
6
*/
7
7
package org .hibernate .type .descriptor .java .spi ;
8
8
9
+ import java .io .Serializable ;
9
10
import java .lang .reflect .ParameterizedType ;
11
+ import java .lang .reflect .Type ;
12
+ import java .util .Map ;
10
13
import java .util .Objects ;
11
14
15
+ import org .hibernate .SharedSessionContract ;
12
16
import org .hibernate .collection .spi .CollectionSemantics ;
17
+ import org .hibernate .collection .spi .MapSemantics ;
13
18
import org .hibernate .collection .spi .PersistentCollection ;
14
19
import org .hibernate .type .descriptor .WrapperOptions ;
15
20
import org .hibernate .type .descriptor .java .AbstractClassJavaType ;
16
21
import org .hibernate .type .descriptor .java .JavaType ;
17
22
import org .hibernate .type .descriptor .java .MutabilityPlan ;
18
- import org .hibernate .type .descriptor .java .MutableMutabilityPlan ;
19
23
import org .hibernate .type .descriptor .jdbc .JdbcType ;
20
24
import org .hibernate .type .descriptor .jdbc .JdbcTypeIndicators ;
21
25
import org .hibernate .type .spi .TypeConfiguration ;
@@ -52,6 +56,9 @@ public JdbcType getRecommendedJdbcType(JdbcTypeIndicators context) {
52
56
public JavaType <C > createJavaType (
53
57
ParameterizedType parameterizedType ,
54
58
TypeConfiguration typeConfiguration ) {
59
+ final Type [] actualTypeArguments = parameterizedType .getActualTypeArguments ();
60
+ final JavaTypeRegistry javaTypeRegistry = typeConfiguration .getJavaTypeRegistry ();
61
+ final JavaType <Object > valueDescriptor = javaTypeRegistry .resolveDescriptor ( actualTypeArguments [actualTypeArguments .length - 1 ] );
55
62
switch ( semantics .getCollectionClassification () ) {
56
63
case ARRAY :
57
64
case BAG :
@@ -63,14 +70,21 @@ public JavaType<C> createJavaType(
63
70
//noinspection unchecked,rawtypes
64
71
return new BasicCollectionJavaType (
65
72
parameterizedType ,
66
- typeConfiguration .getJavaTypeRegistry ()
67
- .resolveDescriptor ( parameterizedType .getActualTypeArguments ()[0 ] ),
73
+ valueDescriptor ,
68
74
semantics
69
75
);
76
+
70
77
}
71
78
// Construct a basic java type that knows its parametrization
72
- //noinspection unchecked
73
- return new UnknownBasicJavaType <>( parameterizedType , (MutabilityPlan <C >) MutableMutabilityPlan .INSTANCE );
79
+ //noinspection unchecked,rawtypes
80
+ return new UnknownBasicJavaType (
81
+ parameterizedType ,
82
+ new MapMutabilityPlan <>(
83
+ (MapSemantics <Map <Object , Object >, Object , Object >) semantics ,
84
+ javaTypeRegistry .resolveDescriptor ( actualTypeArguments [0 ] ),
85
+ valueDescriptor
86
+ )
87
+ );
74
88
}
75
89
76
90
@ Override
@@ -90,30 +104,17 @@ public <X> C wrap(X value, WrapperOptions options) {
90
104
91
105
@ Override
92
106
public boolean areEqual (C one , C another ) {
93
- // return one == another ||
94
- // (
95
- // one instanceof PersistentCollection &&
96
- // ( (PersistentCollection<?>) one ).wasInitialized() &&
97
- // ( (PersistentCollection<?>) one ).isWrapper( another )
98
- // ) ||
99
- // (
100
- // another instanceof PersistentCollection &&
101
- // ( (PersistentCollection<?>) another ).wasInitialized() &&
102
- // ( (PersistentCollection<?>) another ).isWrapper( one )
103
- // );
104
-
105
-
106
107
if ( one == another ) {
107
108
return true ;
108
109
}
109
110
110
- if ( one instanceof PersistentCollection ) {
111
- final PersistentCollection pc = (PersistentCollection ) one ;
111
+ if ( one instanceof PersistentCollection <?> ) {
112
+ final PersistentCollection <?> pc = (PersistentCollection <?> ) one ;
112
113
return pc .wasInitialized () && ( pc .isWrapper ( another ) || pc .isDirectlyProvidedCollection ( another ) );
113
114
}
114
115
115
- if ( another instanceof PersistentCollection ) {
116
- final PersistentCollection pc = (PersistentCollection ) another ;
116
+ if ( another instanceof PersistentCollection <?> ) {
117
+ final PersistentCollection <?> pc = (PersistentCollection <?> ) another ;
117
118
return pc .wasInitialized () && ( pc .isWrapper ( one ) || pc .isDirectlyProvidedCollection ( one ) );
118
119
}
119
120
@@ -124,4 +125,50 @@ public boolean areEqual(C one, C another) {
124
125
public int extractHashCode (C x ) {
125
126
throw new UnsupportedOperationException ();
126
127
}
128
+
129
+ private static class MapMutabilityPlan <C extends Map <K , V >, K , V > implements MutabilityPlan <C > {
130
+
131
+ private final MapSemantics <C , K , V > semantics ;
132
+ private final MutabilityPlan <K > keyPlan ;
133
+ private final MutabilityPlan <V > valuePlan ;
134
+
135
+ public MapMutabilityPlan (
136
+ MapSemantics <C , K , V > semantics ,
137
+ JavaType <K > keyType ,
138
+ JavaType <V > valueType ) {
139
+ this .semantics = semantics ;
140
+ this .keyPlan = keyType .getMutabilityPlan ();
141
+ this .valuePlan = valueType .getMutabilityPlan ();
142
+ }
143
+
144
+ @ Override
145
+ public boolean isMutable () {
146
+ return true ;
147
+ }
148
+
149
+ @ Override
150
+ public C deepCopy (C value ) {
151
+ if ( value == null ) {
152
+ return null ;
153
+ }
154
+ final C copy = semantics .instantiateRaw ( value .size (), null );
155
+
156
+ for ( Map .Entry <K , V > entry : value .entrySet () ) {
157
+ copy .put ( keyPlan .deepCopy ( entry .getKey () ), valuePlan .deepCopy ( entry .getValue () ) );
158
+ }
159
+ return copy ;
160
+ }
161
+
162
+ @ Override
163
+ public Serializable disassemble (C value , SharedSessionContract session ) {
164
+ return (Serializable ) deepCopy ( value );
165
+ }
166
+
167
+ @ Override
168
+ public C assemble (Serializable cached , SharedSessionContract session ) {
169
+ //noinspection unchecked
170
+ return deepCopy ( (C ) cached );
171
+ }
172
+
173
+ }
127
174
}
0 commit comments