HHH-3051 corrected CustomLoader returnTypes (different approach)

Conflicts:
	hibernate-core/src/main/java/org/hibernate/loader/Loader.java
	hibernate-core/src/main/java/org/hibernate/loader/custom/CustomLoader.java
This commit is contained in:
Brett Meyer 2014-01-06 14:12:45 -05:00
parent af1536e9dd
commit 3c673e573c
5 changed files with 52 additions and 41 deletions

View File

@ -939,7 +939,7 @@ public class QueryTranslatorImpl extends BasicLoader implements FilterTranslator
public List list(SessionImplementor session, QueryParameters queryParameters)
throws HibernateException {
return list( session, queryParameters, getQuerySpaces() );
return list( session, queryParameters, getQuerySpaces(), actualReturnTypes );
}
/**
@ -1174,7 +1174,7 @@ public class QueryTranslatorImpl extends BasicLoader implements FilterTranslator
HolderInstantiator hi = HolderInstantiator.createClassicHolderInstantiator(
holderConstructor, queryParameters.getResultTransformer()
);
return scroll( queryParameters, hi, session );
return scroll( queryParameters, returnTypes, hi, session );
}
@Override

View File

@ -2079,16 +2079,7 @@ public abstract class Loader {
protected void autoDiscoverTypes(ResultSet rs) {
throw new AssertionFailure("Auto discover types not supported in this loader");
}
/**
* Return types must be retrieved on-demand, rather than, as an example, provided as an argument on the original
* #list call. They can be overriden later by {@link #autoDiscoverTypes(ResultSet)}, etc.
*
* @return Type[]
*/
protected Type[] getReturnTypes() {
return new Type[0];
}
private ResultSet wrapResultSetIfEnabled(final ResultSet rs, final SessionImplementor session) {
@ -2355,13 +2346,14 @@ public abstract class Loader {
protected List list(
final SessionImplementor session,
final QueryParameters queryParameters,
final Set querySpaces) throws HibernateException {
final Set querySpaces,
final Type[] resultTypes) throws HibernateException {
final boolean cacheable = factory.getSettings().isQueryCacheEnabled() &&
queryParameters.isCacheable();
if ( cacheable ) {
return listUsingQueryCache( session, queryParameters, querySpaces );
return listUsingQueryCache( session, queryParameters, querySpaces, resultTypes );
}
else {
return listIgnoreQueryCache( session, queryParameters );
@ -2375,7 +2367,8 @@ public abstract class Loader {
private List listUsingQueryCache(
final SessionImplementor session,
final QueryParameters queryParameters,
final Set querySpaces) {
final Set querySpaces,
final Type[] resultTypes) {
QueryCache queryCache = factory.getQueryCache( queryParameters.getCacheRegion() );
@ -2391,6 +2384,7 @@ public abstract class Loader {
session,
queryParameters,
querySpaces,
resultTypes,
queryCache,
key
);
@ -2401,6 +2395,7 @@ public abstract class Loader {
putResultInQueryCache(
session,
queryParameters,
resultTypes,
queryCache,
key,
result
@ -2450,17 +2445,17 @@ public abstract class Loader {
final SessionImplementor session,
final QueryParameters queryParameters,
final Set querySpaces,
final Type[] resultTypes,
final QueryCache queryCache,
final QueryKey key) {
List result = null;
final Type[] returnTypes = getReturnTypes();
if ( session.getCacheMode().isGetEnabled() ) {
boolean isImmutableNaturalKeyLookup =
queryParameters.isNaturalKeyLookup() &&
returnTypes.length == 1 &&
returnTypes[0].isEntityType() &&
getEntityPersister( EntityType.class.cast( returnTypes[0] ) )
resultTypes.length == 1 &&
resultTypes[0].isEntityType() &&
getEntityPersister( EntityType.class.cast( resultTypes[0] ) )
.getEntityMetamodel()
.hasImmutableNaturalId();
@ -2479,7 +2474,7 @@ public abstract class Loader {
try {
result = queryCache.get(
key,
key.getResultTransformer().getCachedResultTypes( returnTypes ),
key.getResultTransformer().getCachedResultTypes( resultTypes ),
isImmutableNaturalKeyLookup,
querySpaces,
session
@ -2508,16 +2503,17 @@ public abstract class Loader {
return factory.getEntityPersister( entityType.getAssociatedEntityName() );
}
private void putResultInQueryCache(
protected void putResultInQueryCache(
final SessionImplementor session,
final QueryParameters queryParameters,
final Type[] resultTypes,
final QueryCache queryCache,
final QueryKey key,
final List result) {
if ( session.getCacheMode().isPutEnabled() ) {
boolean put = queryCache.put(
key,
key.getResultTransformer().getCachedResultTypes( getReturnTypes() ),
key.getResultTransformer().getCachedResultTypes( resultTypes ),
result,
queryParameters.isNaturalKeyLookup(),
session
@ -2606,6 +2602,7 @@ public abstract class Loader {
*/
protected ScrollableResults scroll(
final QueryParameters queryParameters,
final Type[] returnTypes,
final HolderInstantiator holderInstantiator,
final SessionImplementor session) throws HibernateException {
@ -2638,7 +2635,7 @@ public abstract class Loader {
session,
this,
queryParameters,
getReturnTypes(),
returnTypes,
holderInstantiator
);
}
@ -2649,7 +2646,7 @@ public abstract class Loader {
session,
this,
queryParameters,
getReturnTypes(),
returnTypes,
holderInstantiator
);
}

View File

@ -112,20 +112,16 @@ public class CriteriaLoader extends OuterJoinLoader {
}
protected Type[] getReturnTypes() {
return resultTypes;
}
public ScrollableResults scroll(SessionImplementor session, ScrollMode scrollMode)
throws HibernateException {
QueryParameters qp = translator.getQueryParameters();
qp.setScrollMode(scrollMode);
return scroll(qp, null, session);
return scroll(qp, resultTypes, null, session);
}
public List list(SessionImplementor session)
throws HibernateException {
return list( session, translator.getQueryParameters(), querySpaces );
return list( session, translator.getQueryParameters(), querySpaces, resultTypes );
}

View File

@ -40,6 +40,8 @@ import org.hibernate.LockOptions;
import org.hibernate.QueryException;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.cache.spi.QueryCache;
import org.hibernate.cache.spi.QueryKey;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.dialect.pagination.LimitHelper;
@ -333,13 +335,9 @@ public class CustomLoader extends Loader {
protected int[] getOwners() {
return entiytOwners;
}
protected Type[] getReturnTypes() {
return resultTypes;
}
public List list(SessionImplementor session, QueryParameters queryParameters) throws HibernateException {
return list( session, queryParameters, querySpaces );
return list( session, queryParameters, querySpaces, resultTypes );
}
@Override
@ -376,6 +374,7 @@ public class CustomLoader extends Loader {
final SessionImplementor session) throws HibernateException {
return scroll(
queryParameters,
resultTypes,
getHolderInstantiator( queryParameters.getResultTransformer(), getReturnAliasesForTransformer() ),
session
);
@ -644,6 +643,28 @@ public class CustomLoader extends Loader {
throw new HibernateException( "Exception while trying to autodiscover types.", e );
}
}
/**
* {@link #resultTypes} can be overridden by {@link #autoDiscoverTypes(ResultSet)},
* *after* {@link #list(SessionImplementor, QueryParameters)} has already been called. It's a bit of a
* chicken-and-the-egg issue since {@link #autoDiscoverTypes(ResultSet)} needs the {@link ResultSet}.
*
* As a hacky workaround, override
* {@link #putResultInQueryCache(SessionImplementor, QueryParameters, Type[], QueryCache, QueryKey, List)} here
* and provide the {@link #resultTypes}.
*
* @see HHH-3051
*/
@Override
protected void putResultInQueryCache(
final SessionImplementor session,
final QueryParameters queryParameters,
final Type[] resultTypes,
final QueryCache queryCache,
final QueryKey key,
final List result) {
super.putResultInQueryCache( session, queryParameters, this.resultTypes, queryCache, key, result );
}
private static class Metadata {
private final SessionFactoryImplementor factory;

View File

@ -492,11 +492,7 @@ public class QueryLoader extends BasicLoader {
SessionImplementor session,
QueryParameters queryParameters) throws HibernateException {
checkQuery( queryParameters );
return list( session, queryParameters, queryTranslator.getQuerySpaces() );
}
protected Type[] getReturnTypes() {
return queryReturnTypes;
return list( session, queryParameters, queryTranslator.getQuerySpaces(), queryReturnTypes );
}
private void checkQuery(QueryParameters queryParameters) {
@ -560,6 +556,7 @@ public class QueryLoader extends BasicLoader {
checkQuery( queryParameters );
return scroll(
queryParameters,
queryReturnTypes,
buildHolderInstantiator( queryParameters.getResultTransformer() ),
session
);