10
10
import org .hibernate .metamodel .model .domain .DomainType ;
11
11
import org .hibernate .metamodel .model .domain .ManagedDomainType ;
12
12
import org .hibernate .metamodel .model .domain .PersistentAttribute ;
13
+ import org .hibernate .metamodel .model .domain .SimpleDomainType ;
13
14
14
15
import java .util .HashMap ;
15
16
import java .util .Map ;
@@ -26,18 +27,30 @@ public class AttributeNodeImpl<J,V,K>
26
27
extends AbstractGraphNode <J >
27
28
implements AttributeNodeImplementor <J > {
28
29
private final PersistentAttribute <?, J > attribute ;
30
+ private final DomainType <V > valueGraphType ;
31
+ private final SimpleDomainType <K > keyGraphType ;
29
32
30
33
private SubGraphImplementor <V > valueSubgraph ;
31
34
private SubGraphImplementor <K > keySubgraph ;
32
35
33
- public <X > AttributeNodeImpl (PersistentAttribute <X , J > attribute , boolean mutable ) {
36
+ static <X ,J > AttributeNodeImpl <J ,?,?> create (PersistentAttribute <X , J > attribute , boolean mutable ) {
37
+ return new AttributeNodeImpl <>( attribute , mutable , attribute .getValueGraphType (), attribute .getKeyGraphType () );
38
+ }
39
+
40
+ private <X > AttributeNodeImpl (
41
+ PersistentAttribute <X , J > attribute , boolean mutable ,
42
+ DomainType <V > valueGraphType , SimpleDomainType <K > keyGraphType ) {
34
43
super ( mutable );
35
44
this .attribute = attribute ;
45
+ this .valueGraphType = valueGraphType ;
46
+ this .keyGraphType = keyGraphType ;
36
47
}
37
48
38
49
private AttributeNodeImpl (AttributeNodeImpl <J ,V ,K > that , boolean mutable ) {
39
50
super ( mutable );
40
51
attribute = that .attribute ;
52
+ valueGraphType = that .valueGraphType ;
53
+ keyGraphType = that .keyGraphType ;
41
54
valueSubgraph = that .valueSubgraph == null ? null : that .valueSubgraph .makeCopy ( mutable );
42
55
keySubgraph = that .keySubgraph == null ? null : that .keySubgraph .makeCopy ( mutable );
43
56
}
@@ -66,21 +79,14 @@ public SubGraphImplementor<K> getKeySubGraph() {
66
79
public SubGraphImplementor <V > makeSubGraph () {
67
80
verifyMutability ();
68
81
if ( valueSubgraph == null ) {
69
- final ManagedDomainType <?> managedType = asManagedType ( attribute .getValueGraphType () );
70
- @ SuppressWarnings ("unchecked" )
71
- final ManagedDomainType <V > valueGraphType = (ManagedDomainType <V >) managedType ;
72
- final SubGraphImplementor <V > graph = new SubGraphImpl <>( valueGraphType , true );
73
- valueSubgraph = graph ;
74
- return graph ;
75
- }
76
- else {
77
- return valueSubgraph ;
82
+ valueSubgraph = new SubGraphImpl <>( asManagedType ( valueGraphType ), true );
78
83
}
84
+ return valueSubgraph ;
79
85
}
80
86
81
87
@ Override
82
88
public <S > SubGraphImplementor <S > makeSubGraph (Class <S > subtype ) {
83
- final ManagedDomainType <? > managedType = asManagedType ( attribute . getValueGraphType () );
89
+ final ManagedDomainType <V > managedType = asManagedType ( valueGraphType );
84
90
if ( !managedType .getBindableJavaType ().isAssignableFrom ( subtype ) ) {
85
91
throw new IllegalArgumentException ( "Not a subtype: " + subtype .getName () );
86
92
}
@@ -93,7 +99,7 @@ public <S> SubGraphImplementor<S> makeSubGraph(Class<S> subtype) {
93
99
94
100
@ Override
95
101
public <S > SubGraphImplementor <S > makeSubGraph (ManagedDomainType <S > subtype ) {
96
- final ManagedDomainType <? > managedType = asManagedType ( attribute . getValueGraphType () );
102
+ final ManagedDomainType <V > managedType = asManagedType ( valueGraphType );
97
103
final Class <S > javaType = subtype .getBindableJavaType ();
98
104
if ( !managedType .getBindableJavaType ().isAssignableFrom ( javaType ) ) {
99
105
throw new IllegalArgumentException ( "Not a subtype: " + javaType .getName () );
@@ -108,22 +114,17 @@ public <S> SubGraphImplementor<S> makeSubGraph(ManagedDomainType<S> subtype) {
108
114
@ Override
109
115
public SubGraphImplementor <K > makeKeySubGraph () {
110
116
verifyMutability ();
117
+ checkMap ();
111
118
if ( keySubgraph == null ) {
112
- final ManagedDomainType <?> managedType = asManagedType ( attribute .getKeyGraphType () );
113
- @ SuppressWarnings ("unchecked" )
114
- final ManagedDomainType <K > keyGraphType = (ManagedDomainType <K >) managedType ;
115
- final SubGraphImplementor <K > graph = new SubGraphImpl <>( keyGraphType , true );
116
- keySubgraph = graph ;
117
- return graph ;
118
- }
119
- else {
120
- return keySubgraph ;
119
+ keySubgraph = new SubGraphImpl <>( asManagedType ( keyGraphType ), true );
121
120
}
121
+ return keySubgraph ;
122
122
}
123
123
124
124
@ Override
125
125
public <S > SubGraphImplementor <S > makeKeySubGraph (Class <S > subtype ) {
126
- final ManagedDomainType <?> type = asManagedType ( attribute .getKeyGraphType () );
126
+ checkMap ();
127
+ final ManagedDomainType <K > type = asManagedType ( keyGraphType );
127
128
if ( !type .getBindableJavaType ().isAssignableFrom ( subtype ) ) {
128
129
throw new IllegalArgumentException ( "Not a key subtype: " + subtype .getName () );
129
130
}
@@ -136,7 +137,8 @@ public <S> SubGraphImplementor<S> makeKeySubGraph(Class<S> subtype) {
136
137
137
138
@ Override
138
139
public <S > SubGraphImplementor <S > makeKeySubGraph (ManagedDomainType <S > subtype ) {
139
- final ManagedDomainType <?> type = asManagedType ( attribute .getKeyGraphType () );
140
+ checkMap ();
141
+ final ManagedDomainType <K > type = asManagedType ( keyGraphType );
140
142
final Class <S > javaType = subtype .getBindableJavaType ();
141
143
if ( !type .getBindableJavaType ().isAssignableFrom ( javaType ) ) {
142
144
throw new IllegalArgumentException ( "Not a key subtype: " + javaType .getName () );
@@ -148,15 +150,32 @@ public <S> SubGraphImplementor<S> makeKeySubGraph(ManagedDomainType<S> subtype)
148
150
return (SubGraphImplementor <S >) result ;
149
151
}
150
152
151
- private static <T > ManagedDomainType <T > asManagedType (DomainType <T > domainType ) {
153
+ private void checkMap () {
154
+ if ( keyGraphType == null ) {
155
+ throw new CannotContainSubGraphException ( "Attribute '" + description () + "' is not a Map" );
156
+ }
157
+ }
158
+
159
+ private <T > ManagedDomainType <T > asManagedType (DomainType <T > domainType ) {
152
160
if ( domainType instanceof ManagedDomainType <T > managedDomainType ) {
153
161
return managedDomainType ;
154
162
}
155
163
else {
156
- throw new CannotContainSubGraphException ( "Not a managed domain type: " + domainType .getTypeName () );
164
+ throw new CannotContainSubGraphException ( "Attribute '" + description ()
165
+ + "' is of type '" + domainType .getTypeName ()
166
+ + "' which is not a managed type" );
157
167
}
158
168
}
159
169
170
+ private String description () {
171
+ return attribute .getDeclaringType ().getTypeName () + "." + attribute .getName ();
172
+ }
173
+
174
+ @ Override
175
+ public String toString () {
176
+ return "AttributeNode[" + description () + "]" ;
177
+ }
178
+
160
179
@ Override
161
180
public AttributeNodeImplementor <J > makeCopy (boolean mutable ) {
162
181
return !mutable && !isMutable () ? this : new AttributeNodeImpl <>( this , mutable );
0 commit comments