/
SqlResultSetProcessor.java
342 lines (311 loc) · 13.4 KB
/
SqlResultSetProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.internal.source.annotations.global;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.logging.Logger;
import org.hibernate.LockMode;
import org.hibernate.MappingException;
import org.hibernate.engine.ResultSetMappingDefinition;
import org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn;
import org.hibernate.engine.query.spi.sql.NativeSQLQueryScalarReturn;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.internal.source.annotations.AnnotationBindingContext;
import org.hibernate.metamodel.internal.source.annotations.util.JPADotNames;
import org.hibernate.metamodel.internal.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.spi.binding.AttributeBinding;
import org.hibernate.metamodel.spi.binding.CompositeAttributeBinding;
import org.hibernate.metamodel.spi.binding.EntityBinding;
import org.hibernate.metamodel.spi.binding.ManyToOneAttributeBinding;
import org.hibernate.metamodel.spi.binding.SingularAssociationAttributeBinding;
import org.hibernate.metamodel.spi.binding.SingularNonAssociationAttributeBinding;
/**
* Binds <ul>
* <li>{@link javax.persistence.SqlResultSetMapping}</li>
* <li>{@link javax.persistence.SqlResultSetMappings}</li>
* <li>{@link javax.persistence.EntityResult}</li>
* <li>{@link javax.persistence.FieldResult}</li>
* <li>{@link javax.persistence.ColumnResult}</li>
* </ul>
*
* @author Strong Liu <stliu@hibernate.org>
*/
public class SqlResultSetProcessor {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
QueryProcessor.class.getName()
);
private SqlResultSetProcessor() {
}
public static void bind(final AnnotationBindingContext bindingContext) {
Collection<AnnotationInstance> annotations = bindingContext.getIndex()
.getAnnotations( JPADotNames.SQL_RESULT_SET_MAPPING );
for ( final AnnotationInstance sqlResultSetMappingAnnotationInstance : annotations ) {
bindSqlResultSetMapping( bindingContext, sqlResultSetMappingAnnotationInstance );
}
annotations = bindingContext.getIndex().getAnnotations( JPADotNames.SQL_RESULT_SET_MAPPINGS );
for ( final AnnotationInstance sqlResultSetMappingsAnnotationInstance : annotations ) {
for ( AnnotationInstance annotationInstance : JandexHelper.getValue(
sqlResultSetMappingsAnnotationInstance,
"value",
AnnotationInstance[].class
) ) {
bindSqlResultSetMapping( bindingContext, annotationInstance );
}
}
}
private static int entityAliasIndex = 0;
private static void bindSqlResultSetMapping(final AnnotationBindingContext bindingContext, final AnnotationInstance annotation) {
entityAliasIndex = 0;
final String name = JandexHelper.getValue( annotation, "name", String.class );
LOG.debugf( "Binding @SqlResultSetMapping(name=%s)", name );
final ResultSetMappingDefinition definition = new ResultSetMappingDefinition( name );
for ( final AnnotationInstance entityResult : JandexHelper.getValue(
annotation,
"entities",
AnnotationInstance[].class
) ) {
bindEntityResult( bindingContext, entityResult, definition );
}
for ( final AnnotationInstance columnResult : JandexHelper.getValue(
annotation,
"columns",
AnnotationInstance[].class
) ) {
bindColumnResult( bindingContext, columnResult, definition );
}
bindingContext.getMetadataImplementor().addResultSetMapping( definition );
}
private static void bindEntityResult(final AnnotationBindingContext bindingContext,
final AnnotationInstance entityResult,
final ResultSetMappingDefinition definition) {
// final Class entityClass = JandexHelper.getValue( entityResult, "entityClass", Class.class );
final String className = JandexHelper.getValue( entityResult, "entityClass", String.class );
//todo look up the whole entitybindings to find the right one seems stupid, but there is no way to look entitybinding
//by class name, since with hbm, hibernate actually supports map one class to multi entities.
final Iterable<EntityBinding> entityBindings = bindingContext.getMetadataImplementor().getEntityBindings();
EntityBinding targetEntityBinding = null;
for ( final EntityBinding entityBinding : entityBindings ) {
if ( className.equals( entityBinding.getEntity().getClassName() ) ) {
targetEntityBinding = entityBinding;
break;
}
}
if ( targetEntityBinding == null ) {
throw new MappingException(
String.format(
"Entity[%s] not found in SqlResultMapping[%s]",
className,
definition.getName()
)
);
}
final String discriminatorColumn = JandexHelper.getValue( entityResult, "discriminatorColumn", String.class );
final Map<String, String[]> propertyResults = new HashMap<String, String[]>();
if ( StringHelper.isNotEmpty( discriminatorColumn ) ) {
final String quotingNormalizedName = bindingContext.getMetadataImplementor()
.getObjectNameNormalizer()
.normalizeIdentifierQuoting(
discriminatorColumn
);
propertyResults.put( "class", new String[] { quotingNormalizedName } );
}
List<FieldResult> fieldResultList = reorderFieldResult(
bindingContext,
entityResult,
targetEntityBinding,
definition.getName()
);
for ( final FieldResult fieldResult : fieldResultList ) {
insert( fieldResult.column, StringHelper.root( fieldResult.name ), propertyResults );
}
final NativeSQLQueryRootReturn result = new NativeSQLQueryRootReturn(
"alias" + entityAliasIndex++,
targetEntityBinding.getEntity().getName(),
propertyResults,
LockMode.READ
);
definition.addQueryReturn( result );
}
//todo see org.hibernate.cfg.annotations.ResultsetMappingSecondPass#getSubPropertyIterator
private static List<FieldResult> reorderFieldResult(AnnotationBindingContext bindingContext,
AnnotationInstance entityResult,
EntityBinding entityBinding,
String resultSetMappingDefinitionName) {
final AnnotationInstance[] fieldResultAnnotationInstances = JandexHelper.getValue(
entityResult,
"fields",
AnnotationInstance[].class
);
List<FieldResult> results = new ArrayList<FieldResult>( fieldResultAnnotationInstances.length );
List<String> propertyNames = new ArrayList<String>();
final Set<String> uniqueReturnProperty = new HashSet<String>();
for ( final AnnotationInstance fieldResult : fieldResultAnnotationInstances ) {
final String name = JandexHelper.getValue( fieldResult, "name", String.class );
if ( !uniqueReturnProperty.add( name ) ) {
throw new MappingException(
"duplicate @FieldResult for property " + name +
" on @Entity " + entityBinding.getEntity()
.getName() + " in " + resultSetMappingDefinitionName
);
}
if ( "class".equals( name ) ) {
throw new MappingException(
"class is not a valid property name to use in a @FieldResult, use @EntityResult(discriminatorColumn) instead"
);
}
final String column = JandexHelper.getValue( fieldResult, "column", String.class );
final String quotingNormalizedColumnName = normalize( bindingContext, column );
if ( name.contains( "." ) ) {
int dotIndex = name.lastIndexOf( '.' );
String reducedName = name.substring( 0, dotIndex );
AttributeBinding attributeBinding = entityBinding.locateAttributeBinding( reducedName );
Iterable<? extends AttributeBinding> attributeBindings = null;
if ( CompositeAttributeBinding.class.isInstance( attributeBinding ) ) {
CompositeAttributeBinding compositeAttributeBinding = CompositeAttributeBinding.class.cast(
attributeBinding
);
attributeBindings = compositeAttributeBinding.attributeBindings();
}
else if ( ManyToOneAttributeBinding.class.isInstance( attributeBinding ) ) {
ManyToOneAttributeBinding manyToOneAttributeBinding = ManyToOneAttributeBinding.class.cast(
attributeBinding
);
EntityBinding referencedEntityBinding = manyToOneAttributeBinding.getReferencedEntityBinding();
Set<SingularAssociationAttributeBinding> referencingAttributeBindings = manyToOneAttributeBinding.getEntityReferencingAttributeBindings();
if ( CollectionHelper.isNotEmpty( referencingAttributeBindings ) ) {
attributeBindings = referencingAttributeBindings;
}
else {
// EntityIdentifierNature entityIdentifierNature= referencedEntityBinding.getHierarchyDetails().getEntityIdentifier().getNature();
// switch ( entityIdentifierNature ) {
// case SIMPLE:
// throw new MappingException(
// "dotted notation reference neither a component nor a many/one to one"
// );
// case AGGREGATED_COMPOSITE:
// case COMPOSITE:
// referencedEntityBinding.getHierarchyDetails().getEntityIdentifier().isSingleAttribute();//.isIdentifierMapper();
// }
//todo check if this logic is correct
SingularNonAssociationAttributeBinding identifierAttributeBinding = referencedEntityBinding.getHierarchyDetails()
.getEntityIdentifier()
.getAttributeBinding();
if ( CompositeAttributeBinding.class.isInstance( identifierAttributeBinding ) ) {
attributeBindings = CompositeAttributeBinding.class.cast( identifierAttributeBinding )
.attributeBindings();
}
else {
throw new MappingException(
"dotted notation reference neither a component nor a many/one to one"
);
}
}
}
else {
throw new MappingException( "dotted notation reference neither a component nor a many/one to one" );
}
List<String> followers = getFollowers( attributeBindings, reducedName, name );
int index = results.size();
int followersSize = followers.size();
for ( int loop = 0; loop < followersSize; loop++ ) {
String follower = followers.get( loop );
int currentIndex = getIndexOfFirstMatchingProperty( propertyNames, follower );
index = currentIndex != -1 && currentIndex < index ? currentIndex : index;
}
propertyNames.add( index, name );
results.add( index, new FieldResult( name, quotingNormalizedColumnName ) );
}
else {
propertyNames.add( name );
results.add( new FieldResult( name, quotingNormalizedColumnName ) );
}
}
return results;
}
private static int getIndexOfFirstMatchingProperty(List<String> propertyNames, String follower) {
int propertySize = propertyNames.size();
for ( int propIndex = 0; propIndex < propertySize; propIndex++ ) {
if ( ( propertyNames.get( propIndex ) ).startsWith( follower ) ) {
return propIndex;
}
}
return -1;
}
private static class FieldResult {
String name;
String column;
private FieldResult(String column, String name) {
this.column = column;
this.name = name;
}
}
private static List<String> getFollowers(Iterable<? extends AttributeBinding> attributeBindings, String reducedName, String name) {
boolean hasFollowers = false;
List<String> followers = new ArrayList<String>();
for ( final AttributeBinding attributeBinding : attributeBindings ) {
String currentPropertyName = attributeBinding.getAttribute().getName();
String currentName = reducedName + '.' + currentPropertyName;
if ( hasFollowers ) {
followers.add( currentName );
}
if ( name.equals( currentName ) ) {
hasFollowers = true;
}
}
return followers;
}
private static void insert(String key, String value, Map<String, String[]> map) {
if ( map.containsKey( key ) ) {
String[] oldValues = map.get( key );
String[] values = Arrays.copyOf( oldValues, oldValues.length + 1 );
values[oldValues.length] = value;
map.put( key, values );
}
else {
map.put( key, new String[] { value } );
}
}
private static void bindColumnResult(final AnnotationBindingContext bindingContext,
final AnnotationInstance columnResult,
final ResultSetMappingDefinition definition) {
final String name = JandexHelper.getValue( columnResult, "name", String.class );
final String normalizedName = normalize( bindingContext, name );
definition.addQueryReturn( new NativeSQLQueryScalarReturn( normalizedName, null ) );
}
private static String normalize(final AnnotationBindingContext bindingContext, String name) {
return bindingContext.getMetadataImplementor()
.getObjectNameNormalizer()
.normalizeIdentifierQuoting( name );
}
}