HHH-13508 Reuse alias names generated by BasicLoader#generateSuffixes

This commit is contained in:
Sanne Grinovero 2019-07-25 12:27:27 +01:00 committed by Andrea Boriero
parent ed8c72871b
commit 1da6bb6995
3 changed files with 50 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.List;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.loader.internal.AliasConstantsHelper;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.Loadable;
import org.hibernate.type.BagType;
@ -100,7 +101,7 @@ public abstract class BasicLoader extends Loader {
String[] suffixes = new String[length];
for ( int i = 0; i < length; i++ ) {
suffixes[i] = Integer.toString( i + seed ) + '_';
suffixes[i] = AliasConstantsHelper.get( i + seed );
}
return suffixes;
}

View File

@ -37,6 +37,7 @@ import org.hibernate.hql.spi.ParameterInformation;
import org.hibernate.internal.IteratorImpl;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.loader.BasicLoader;
import org.hibernate.loader.internal.AliasConstantsHelper;
import org.hibernate.loader.spi.AfterLoadAction;
import org.hibernate.param.ParameterSpecification;
import org.hibernate.persister.collection.CollectionPersister;
@ -160,7 +161,7 @@ public class QueryLoader extends BasicLoader {
entityAliases[i] = element.getClassAlias();
sqlAliasByEntityAlias.put( entityAliases[i], sqlAliases[i] );
// TODO should we just collect these like with the collections above?
sqlAliasSuffixes[i] = ( size == 1 ) ? "" : (Integer.toString( i ) + '_');
sqlAliasSuffixes[i] = ( size == 1 ) ? "" : AliasConstantsHelper.get( i );
// sqlAliasSuffixes[i] = element.getColumnAliasSuffix();
includeInSelect[i] = !element.isFetch();
if ( includeInSelect[i] ) {

View File

@ -0,0 +1,46 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.loader.internal;
/**
* @author Sanne Grinovero
*/
public final class AliasConstantsHelper {
private static final int MAX_POOL_SIZE = 40;
private static final String[] pool = initPool( MAX_POOL_SIZE );
/**
* Returns the same as Integer.toString( i ) + '_'
* Strings might be returned from a pool of constants, when i
* is within the range of expected most commonly requested elements.
*
* @param i
* @return
*/
public static String get(final int i) {
if ( i < MAX_POOL_SIZE && i >= 0 ) {
return pool[i];
}
else {
return internalAlias( i );
}
}
private static String[] initPool(final int maxPoolSize) {
String[] pool = new String[maxPoolSize];
for ( int i = 0; i < maxPoolSize; i++ ) {
pool[i] = internalAlias( i );
}
return pool;
}
private static String internalAlias(final int i) {
return Integer.toString( i ) + '_';
}
}