HHH-3051 corrected CustomLoader returnTypes (different approach)

This commit is contained in:
Brett Meyer 2014-01-06 12:38:12 -05:00
parent 99a6fc07b6
commit b3e79f3f40
5 changed files with 52 additions and 41 deletions

View File

@ -936,7 +936,7 @@ public class QueryTranslatorImpl extends BasicLoader implements FilterTranslator
@Override
public List list(SessionImplementor session, QueryParameters queryParameters)
throws HibernateException {
return list( session, queryParameters, getQuerySpaces() );
return list( session, queryParameters, getQuerySpaces(), actualReturnTypes );
}
/**
@ -1172,7 +1172,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

@ -2082,16 +2082,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) {
@ -2358,13 +2349,14 @@ public abstract class Loader {
protected List list(
final SessionImplementor session,
final QueryParameters queryParameters,
final Set<Serializable> querySpaces) throws HibernateException {
final Set<Serializable> 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 );
@ -2378,7 +2370,8 @@ public abstract class Loader {
private List listUsingQueryCache(
final SessionImplementor session,
final QueryParameters queryParameters,
final Set<Serializable> querySpaces) {
final Set<Serializable> querySpaces,
final Type[] resultTypes) {
QueryCache queryCache = factory.getQueryCache( queryParameters.getCacheRegion() );
@ -2394,6 +2387,7 @@ public abstract class Loader {
session,
queryParameters,
querySpaces,
resultTypes,
queryCache,
key
);
@ -2404,6 +2398,7 @@ public abstract class Loader {
putResultInQueryCache(
session,
queryParameters,
resultTypes,
queryCache,
key,
result
@ -2453,17 +2448,17 @@ public abstract class Loader {
final SessionImplementor session,
final QueryParameters queryParameters,
final Set<Serializable> 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();
@ -2482,7 +2477,7 @@ public abstract class Loader {
try {
result = queryCache.get(
key,
key.getResultTransformer().getCachedResultTypes( returnTypes ),
key.getResultTransformer().getCachedResultTypes( resultTypes ),
isImmutableNaturalKeyLookup,
querySpaces,
session
@ -2511,16 +2506,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
@ -2609,6 +2605,7 @@ public abstract class Loader {
*/
protected ScrollableResults scroll(
final QueryParameters queryParameters,
final Type[] returnTypes,
final HolderInstantiator holderInstantiator,
final SessionImplementor session) throws HibernateException {
@ -2641,7 +2638,7 @@ public abstract class Loader {
session,
this,
queryParameters,
getReturnTypes(),
returnTypes,
holderInstantiator
);
}
@ -2652,7 +2649,7 @@ public abstract class Loader {
session,
this,
queryParameters,
getReturnTypes(),
returnTypes,
holderInstantiator
);
}

View File

@ -114,20 +114,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 );
}
@Override

View File

@ -38,6 +38,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.engine.spi.QueryParameters;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -346,13 +348,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
@ -388,6 +386,7 @@ public class CustomLoader extends Loader {
throws HibernateException {
return scroll(
queryParameters,
resultTypes,
getHolderInstantiator( queryParameters.getResultTransformer(), getReturnAliasesForTransformer() ),
session
);
@ -532,5 +531,27 @@ public class CustomLoader extends Loader {
@SuppressWarnings("UnusedParameters")
protected void validateAlias(String alias) {
}
/**
* {@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 );
}
}

View File

@ -493,11 +493,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) {
@ -561,6 +557,7 @@ public class QueryLoader extends BasicLoader {
checkQuery( queryParameters );
return scroll(
queryParameters,
queryReturnTypes,
buildHolderInstantiator( queryParameters.getResultTransformer() ),
session
);