Skip to content

Commit 95d66ed

Browse files
committed
HHH-8893 HBM transform/mock
1 parent 87a4b56 commit 95d66ed

39 files changed

+868
-624
lines changed

hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ public static String unqualifyEntityName(String entityName) {
610610
}
611611

612612
public static String toUpperCase(String str) {
613-
return str==null ? null : str.toUpperCase();
613+
return str == null ? null : str.toUpperCase(Locale.ENGLISH);
614614
}
615615

616616
public static String toLowerCase(String str) {

hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/annotations/global/QueryProcessor.java

Lines changed: 126 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.Collection;
2727
import java.util.HashMap;
28+
2829
import javax.persistence.LockModeType;
2930
import javax.persistence.ParameterMode;
3031

@@ -41,7 +42,6 @@
4142
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
4243
import org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn;
4344
import org.hibernate.engine.spi.NamedQueryDefinitionBuilder;
44-
import org.hibernate.engine.spi.NamedSQLQueryDefinition;
4545
import org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder;
4646
import org.hibernate.internal.CoreLogging;
4747
import org.hibernate.internal.CoreMessageLogger;
@@ -52,7 +52,6 @@
5252
import org.hibernate.metamodel.source.internal.annotations.util.HibernateDotNames;
5353
import org.hibernate.metamodel.source.internal.annotations.util.JPADotNames;
5454
import org.hibernate.metamodel.source.internal.annotations.util.JandexHelper;
55-
5655
import org.jboss.jandex.AnnotationInstance;
5756
import org.jboss.jandex.AnnotationValue;
5857

@@ -113,7 +112,7 @@ public static void bind(AnnotationBindingContext bindingContext) {
113112
JPADotNames.NAMED_NATIVE_QUERIES
114113
);
115114
for ( AnnotationInstance query : annotations ) {
116-
bindNamedNativeQuery( query, bindingContext );
115+
bindNamedNativeQuery( bindingContext, query );
117116
}
118117

119118
annotations = JandexHelper.collectionAnnotations(
@@ -122,7 +121,7 @@ public static void bind(AnnotationBindingContext bindingContext) {
122121
JPADotNames.NAMED_STORED_PROCEDURE_QUERIES
123122
);
124123
for ( AnnotationInstance query : annotations ) {
125-
bindNamedStoredProcedureQuery( query, bindingContext );
124+
bindNamedStoredProcedureQuery( bindingContext, query );
126125
}
127126

128127
annotations = JandexHelper.collectionAnnotations(
@@ -140,7 +139,7 @@ public static void bind(AnnotationBindingContext bindingContext) {
140139
HibernateDotNames.NAMED_NATIVE_QUERIES
141140
);
142141
for ( AnnotationInstance query : annotations ) {
143-
bindNamedNativeQuery( query, bindingContext );
142+
bindNamedNativeQuery( bindingContext, query );
144143
}
145144
}
146145

@@ -161,8 +160,7 @@ private static void bindNamedQuery(AnnotationBindingContext bindingContext, Anno
161160
if ( annotation.name().equals( JPADotNames.NAMED_QUERY ) ) {
162161
bindJPANamedQuery( annotation, builder, name, query, bindingContext );
163162
} else {
164-
builder.setFlushMode(
165-
getFlushMode( JandexHelper.getEnumValue( annotation, "flushMode", FlushModeType.class, classLoaderService ) ) )
163+
builder.setFlushMode( getFlushMode( JandexHelper.getEnumValue( annotation, "flushMode", FlushModeType.class, classLoaderService ) ) )
166164
.setCacheable( JandexHelper.getValue( annotation, "cacheable", Boolean.class, classLoaderService ) )
167165
.setCacheRegion( defaultToNull( JandexHelper.getValue( annotation, "cacheRegion", String.class, classLoaderService ) ) )
168166
.setFetchSize( defaultToNull( JandexHelper.getValue( annotation, "fetchSize", Integer.class, classLoaderService ) ) )
@@ -172,52 +170,10 @@ private static void bindNamedQuery(AnnotationBindingContext bindingContext, Anno
172170
.setReadOnly( JandexHelper.getValue( annotation, "readOnly", Boolean.class, classLoaderService ) );
173171
}
174172

175-
176173
bindingContext.getMetadataCollector().addNamedQuery(builder.createNamedQueryDefinition());
177174
LOG.debugf( "Binding named query: %s => %s", name, query );
178175
}
179176

180-
public static FlushMode getFlushMode(FlushModeType flushModeType) {
181-
FlushMode flushMode;
182-
switch ( flushModeType ) {
183-
case ALWAYS:
184-
flushMode = FlushMode.ALWAYS;
185-
break;
186-
case AUTO:
187-
flushMode = FlushMode.AUTO;
188-
break;
189-
case COMMIT:
190-
flushMode = FlushMode.COMMIT;
191-
break;
192-
case MANUAL:
193-
flushMode = FlushMode.MANUAL;
194-
break;
195-
case PERSISTENCE_CONTEXT:
196-
flushMode = null;
197-
break;
198-
default:
199-
throw new AssertionFailure( "Unknown flushModeType: " + flushModeType );
200-
}
201-
return flushMode;
202-
}
203-
private static CacheMode getCacheMode(CacheModeType cacheModeType) {
204-
switch ( cacheModeType ) {
205-
case GET:
206-
return CacheMode.GET;
207-
case IGNORE:
208-
return CacheMode.IGNORE;
209-
case NORMAL:
210-
return CacheMode.NORMAL;
211-
case PUT:
212-
return CacheMode.PUT;
213-
case REFRESH:
214-
return CacheMode.REFRESH;
215-
default:
216-
throw new AssertionFailure( "Unknown cacheModeType: " + cacheModeType );
217-
}
218-
}
219-
220-
221177
private static void bindJPANamedQuery(
222178
AnnotationInstance annotation,
223179
NamedQueryDefinitionBuilder builder,
@@ -263,21 +219,84 @@ private static void bindJPANamedQuery(
263219
.setComment( defaultToNull( getString( hints, QueryHints.COMMENT, bindingContext ) ) )
264220
.setParameterTypes( null );
265221
}
266-
267-
private static void bindNamedNativeQuery(AnnotationInstance annotation, AnnotationBindingContext bindingContext) {
222+
223+
private static void bindNamedNativeQuery(AnnotationBindingContext bindingContext, AnnotationInstance annotation) {
268224
final ClassLoaderService classLoaderService = bindingContext.getBuildingOptions().getServiceRegistry().getService( ClassLoaderService.class );
269-
String name = JandexHelper.getValue( annotation, "name", String.class, classLoaderService );
225+
final String name = JandexHelper.getValue( annotation, "name", String.class, classLoaderService );
270226
if ( StringHelper.isEmpty( name ) ) {
271227
throw new AnnotationException( "A named native query must have a name when used in class or package level" );
272228
}
229+
NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder();
230+
builder.setName( name );
273231

274-
String query = JandexHelper.getValue( annotation, "query", String.class, classLoaderService );
232+
final String query = JandexHelper.getValue( annotation, "query", String.class, classLoaderService );
233+
builder.setQuery( query );
234+
235+
if ( annotation.name().equals( JPADotNames.NAMED_NATIVE_QUERY ) ) {
236+
bindJPANamedNativeQuery( annotation, builder, name, query, bindingContext );
237+
238+
final String resultSetMapping = JandexHelper.getValue(
239+
annotation, "resultSetMapping", String.class, classLoaderService );
240+
if ( StringHelper.isNotEmpty( resultSetMapping ) ) {
241+
boolean resultSetMappingExists = bindingContext.getMetadataCollector().getResultSetMappingDefinitions().containsKey( resultSetMapping );
242+
if ( !resultSetMappingExists ) {
243+
throw new MappingException(
244+
String.format(
245+
"Named SQL Query [%s] referenced an non-existent result set mapping [%s] ",
246+
name,
247+
resultSetMapping
248+
)
249+
);
250+
}
251+
builder.setResultSetRef( resultSetMapping );
252+
}
253+
else {
254+
AnnotationValue annotationValue = annotation.value( "resultClass" );
255+
NativeSQLQueryRootReturn[] queryRoots;
256+
if ( annotationValue == null ) {
257+
// pure native scalar query
258+
queryRoots = new NativeSQLQueryRootReturn[0];
259+
}
260+
else {
261+
queryRoots = new NativeSQLQueryRootReturn[] {
262+
new NativeSQLQueryRootReturn(
263+
"alias1",
264+
annotationValue.asString(),
265+
new HashMap<String, String[]>(),
266+
LockMode.READ
267+
)
268+
};
269+
}
270+
builder.setQueryReturns( queryRoots );
271+
}
272+
}
273+
else {
274+
builder.setFlushMode( getFlushMode( JandexHelper.getEnumValue( annotation, "flushMode", FlushModeType.class, classLoaderService ) ) )
275+
.setCacheable( JandexHelper.getValue( annotation, "cacheable", Boolean.class, classLoaderService ) )
276+
.setCacheRegion( defaultToNull( JandexHelper.getValue( annotation, "cacheRegion", String.class, classLoaderService ) ) )
277+
.setFetchSize( defaultToNull( JandexHelper.getValue( annotation, "fetchSize", Integer.class, classLoaderService ) ) )
278+
.setTimeout( defaultToNull( JandexHelper.getValue( annotation, "timeout", Integer.class, classLoaderService ) ) )
279+
.setComment( JandexHelper.getValue( annotation, "comment", String.class, classLoaderService ) )
280+
.setCacheMode( getCacheMode( JandexHelper.getValue( annotation, "cacheMode", CacheModeType.class, classLoaderService ) ) )
281+
.setReadOnly( JandexHelper.getValue( annotation, "readOnly", Boolean.class, classLoaderService ) )
282+
.setCallable( JandexHelper.getValue( annotation, "callable", Boolean.class, classLoaderService ) )
283+
.setQueryReturns( new NativeSQLQueryRootReturn[0] );
284+
}
275285

276-
String resultSetMapping = JandexHelper.getValue( annotation, "resultSetMapping", String.class, classLoaderService );
286+
bindingContext.getMetadataCollector().addNamedNativeQuery(builder.createNamedQueryDefinition());
287+
LOG.debugf( "Binding named query: %s => %s", name, query );
288+
}
277289

278-
AnnotationInstance[] hints = JandexHelper.getValue( annotation, "hints", AnnotationInstance[].class, classLoaderService );
290+
private static void bindJPANamedNativeQuery(
291+
AnnotationInstance annotation,
292+
NamedSQLQueryDefinitionBuilder builder,
293+
String name,
294+
String query,
295+
AnnotationBindingContext bindingContext){
296+
final ClassLoaderService classLoaderService = bindingContext.getBuildingOptions().getServiceRegistry().getService( ClassLoaderService.class );
297+
AnnotationInstance[] hints = JandexHelper.getValue( annotation, "hints", AnnotationInstance[].class,
298+
classLoaderService );
279299

280-
boolean cacheable = getBoolean( hints, "org.hibernate.cacheable", name, bindingContext );
281300
String cacheRegion = getString( hints, QueryHints.CACHE_REGION, bindingContext );
282301
if ( StringHelper.isEmpty( cacheRegion ) ) {
283302
cacheRegion = null;
@@ -287,93 +306,63 @@ private static void bindNamedNativeQuery(AnnotationInstance annotation, Annotati
287306
if ( timeout != null && timeout < 0 ) {
288307
timeout = null;
289308
}
309+
310+
builder.setCacheable( getBoolean( hints, QueryHints.CACHEABLE, name, bindingContext ) )
311+
.setCacheRegion( cacheRegion )
312+
.setTimeout( timeout )
313+
.setFetchSize( defaultToNull( getInteger( hints, QueryHints.FETCH_SIZE, name, bindingContext ) ) )
314+
.setFlushMode( getFlushMode( hints, QueryHints.FLUSH_MODE, name, bindingContext ) )
315+
.setCacheMode( getCacheMode( hints, QueryHints.CACHE_MODE, name, bindingContext ) )
316+
.setReadOnly( getBoolean( hints, QueryHints.READ_ONLY, name, bindingContext ) )
317+
.setComment( defaultToNull( getString( hints, QueryHints.COMMENT, bindingContext ) ) )
318+
.setParameterTypes( null )
319+
.setCallable( getBoolean( hints, QueryHints.CALLABLE, name, bindingContext ) );
320+
}
290321

291-
Integer fetchSize = getInteger( hints, QueryHints.FETCH_SIZE, name, bindingContext );
292-
if ( fetchSize != null && fetchSize < 0 ) {
293-
fetchSize = null;
294-
}
295-
296-
FlushMode flushMode = getFlushMode( hints, QueryHints.FLUSH_MODE, name, bindingContext );
297-
CacheMode cacheMode = getCacheMode( hints, QueryHints.CACHE_MODE, name, bindingContext );
298-
299-
boolean readOnly = getBoolean( hints, QueryHints.READ_ONLY, name, bindingContext );
300-
301-
String comment = getString( hints, QueryHints.COMMENT, bindingContext );
302-
if ( StringHelper.isEmpty( comment ) ) {
303-
comment = null;
322+
public static FlushMode getFlushMode(FlushModeType flushModeType) {
323+
FlushMode flushMode;
324+
switch ( flushModeType ) {
325+
case ALWAYS:
326+
flushMode = FlushMode.ALWAYS;
327+
break;
328+
case AUTO:
329+
flushMode = FlushMode.AUTO;
330+
break;
331+
case COMMIT:
332+
flushMode = FlushMode.COMMIT;
333+
break;
334+
case MANUAL:
335+
flushMode = FlushMode.MANUAL;
336+
break;
337+
case PERSISTENCE_CONTEXT:
338+
flushMode = null;
339+
break;
340+
default:
341+
throw new AssertionFailure( "Unknown flushModeType: " + flushModeType );
304342
}
305-
306-
boolean callable = getBoolean( hints, QueryHints.CALLABLE, name, bindingContext );
307-
NamedSQLQueryDefinition def;
308-
if ( StringHelper.isNotEmpty( resultSetMapping ) ) {
309-
boolean resultSetMappingExists = bindingContext.getMetadataCollector().getResultSetMappingDefinitions().containsKey( resultSetMapping );
310-
if ( !resultSetMappingExists ) {
311-
throw new MappingException(
312-
String.format(
313-
"Named SQL Query [%s] referenced an non-existent result set mapping [%s] ",
314-
name,
315-
resultSetMapping
316-
)
317-
);
318-
}
319-
def = new NamedSQLQueryDefinitionBuilder().setName( name )
320-
.setQuery( query )
321-
.setResultSetRef(
322-
resultSetMapping
323-
)
324-
.setQuerySpaces( null )
325-
.setCacheable( cacheable )
326-
.setCacheRegion( cacheRegion )
327-
.setTimeout( timeout )
328-
.setFetchSize( fetchSize )
329-
.setFlushMode( flushMode )
330-
.setCacheMode( cacheMode )
331-
.setReadOnly( readOnly )
332-
.setComment( comment )
333-
.setParameterTypes( null )
334-
.setCallable( callable )
335-
.createNamedQueryDefinition();
343+
return flushMode;
344+
}
345+
346+
private static CacheMode getCacheMode(CacheModeType cacheModeType) {
347+
switch ( cacheModeType ) {
348+
case GET:
349+
return CacheMode.GET;
350+
case IGNORE:
351+
return CacheMode.IGNORE;
352+
case NORMAL:
353+
return CacheMode.NORMAL;
354+
case PUT:
355+
return CacheMode.PUT;
356+
case REFRESH:
357+
return CacheMode.REFRESH;
358+
default:
359+
throw new AssertionFailure( "Unknown cacheModeType: " + cacheModeType );
336360
}
337-
else {
338-
AnnotationValue annotationValue = annotation.value( "resultClass" );
339-
NativeSQLQueryRootReturn[] queryRoots;
340-
if ( annotationValue == null ) {
341-
// pure native scalar query
342-
queryRoots = new NativeSQLQueryRootReturn[0];
343-
}
344-
else {
345-
queryRoots = new NativeSQLQueryRootReturn[] {
346-
new NativeSQLQueryRootReturn(
347-
"alias1",
348-
annotationValue.asString(),
349-
new HashMap<String, String[]>(),
350-
LockMode.READ
351-
)
352-
};
353-
}
354-
def = new NamedSQLQueryDefinitionBuilder().setName( name )
355-
.setQuery( query )
356-
.setQueryReturns( queryRoots )
357-
.setQuerySpaces( null )
358-
.setCacheable( cacheable )
359-
.setCacheRegion( cacheRegion )
360-
.setTimeout( timeout )
361-
.setFetchSize( fetchSize )
362-
.setFlushMode( flushMode )
363-
.setCacheMode( cacheMode )
364-
.setReadOnly( readOnly )
365-
.setComment( comment )
366-
.setParameterTypes( null )
367-
.setCallable( callable )
368-
.createNamedQueryDefinition();
369-
}
370-
bindingContext.getMetadataCollector().addNamedNativeQuery( def );
371-
LOG.debugf( "Binding named native query: %s => %s", name, query );
372361
}
373362

374363
private static void bindNamedStoredProcedureQuery(
375-
AnnotationInstance query,
376-
AnnotationBindingContext bindingContext) {
364+
AnnotationBindingContext bindingContext,
365+
AnnotationInstance query) {
377366
final String name = query.value( "name" ).asString();
378367
final String procedureName = query.value( "procedureName" ).asString();
379368
LOG.debugf( "Starting binding of @NamedStoredProcedureQuery(name=%s, procedureName=%s)", name, procedureName );

hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/jandex/AbstractAttributesBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@
5050
*/
5151
public abstract class AbstractAttributesBuilder {
5252

53-
private ClassInfo classInfo;
54-
private EntityMappingsMocker.Default defaults;
55-
private IndexBuilder indexBuilder;
53+
protected ClassInfo classInfo;
54+
protected Default defaults;
55+
protected IndexBuilder indexBuilder;
5656

57-
AbstractAttributesBuilder(IndexBuilder indexBuilder, ClassInfo classInfo, EntityMappingsMocker.Default defaults) {
57+
AbstractAttributesBuilder(IndexBuilder indexBuilder, ClassInfo classInfo, Default defaults) {
5858
this.indexBuilder = indexBuilder;
5959
this.classInfo = classInfo;
6060
this.defaults = defaults;
6161
}
6262

63-
final void parse() {
63+
protected void parse() {
6464
for ( JaxbId id : getId() ) {
6565
new IdMocker( indexBuilder, classInfo, defaults, id ).process();
6666
}

0 commit comments

Comments
 (0)