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) public List list(SessionImplementor session, QueryParameters queryParameters)
throws HibernateException { 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( HolderInstantiator hi = HolderInstantiator.createClassicHolderInstantiator(
holderConstructor, queryParameters.getResultTransformer() holderConstructor, queryParameters.getResultTransformer()
); );
return scroll( queryParameters, hi, session ); return scroll( queryParameters, returnTypes, hi, session );
} }
@Override @Override

View File

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

View File

@ -112,20 +112,16 @@ public class CriteriaLoader extends OuterJoinLoader {
} }
protected Type[] getReturnTypes() {
return resultTypes;
}
public ScrollableResults scroll(SessionImplementor session, ScrollMode scrollMode) public ScrollableResults scroll(SessionImplementor session, ScrollMode scrollMode)
throws HibernateException { throws HibernateException {
QueryParameters qp = translator.getQueryParameters(); QueryParameters qp = translator.getQueryParameters();
qp.setScrollMode(scrollMode); qp.setScrollMode(scrollMode);
return scroll(qp, null, session); return scroll(qp, resultTypes, null, session);
} }
public List list(SessionImplementor session) public List list(SessionImplementor session)
throws HibernateException { 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.QueryException;
import org.hibernate.ScrollableResults; import org.hibernate.ScrollableResults;
import org.hibernate.Session; 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.Dialect;
import org.hibernate.dialect.pagination.LimitHandler; import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.dialect.pagination.LimitHelper; import org.hibernate.dialect.pagination.LimitHelper;
@ -333,13 +335,9 @@ public class CustomLoader extends Loader {
protected int[] getOwners() { protected int[] getOwners() {
return entiytOwners; return entiytOwners;
} }
protected Type[] getReturnTypes() {
return resultTypes;
}
public List list(SessionImplementor session, QueryParameters queryParameters) throws HibernateException { public List list(SessionImplementor session, QueryParameters queryParameters) throws HibernateException {
return list( session, queryParameters, querySpaces ); return list( session, queryParameters, querySpaces, resultTypes );
} }
@Override @Override
@ -376,6 +374,7 @@ public class CustomLoader extends Loader {
final SessionImplementor session) throws HibernateException { final SessionImplementor session) throws HibernateException {
return scroll( return scroll(
queryParameters, queryParameters,
resultTypes,
getHolderInstantiator( queryParameters.getResultTransformer(), getReturnAliasesForTransformer() ), getHolderInstantiator( queryParameters.getResultTransformer(), getReturnAliasesForTransformer() ),
session session
); );
@ -644,6 +643,28 @@ public class CustomLoader extends Loader {
throw new HibernateException( "Exception while trying to autodiscover types.", e ); 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 static class Metadata {
private final SessionFactoryImplementor factory; private final SessionFactoryImplementor factory;

View File

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