HHH-3051 corrected Loader returnTypes

Conflicts:
	hibernate-core/src/main/java/org/hibernate/loader/Loader.java
This commit is contained in:
Brett Meyer 2014-01-02 15:14:59 -05:00
parent 7b19ea2c88
commit 7200540651
6 changed files with 86 additions and 27 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(), actualReturnTypes );
return list( session, queryParameters, getQuerySpaces() );
}
/**
@ -1174,7 +1174,7 @@ public class QueryTranslatorImpl extends BasicLoader implements FilterTranslator
HolderInstantiator hi = HolderInstantiator.createClassicHolderInstantiator(
holderConstructor, queryParameters.getResultTransformer()
);
return scroll( queryParameters, returnTypes, hi, session );
return scroll( queryParameters, hi, session );
}
@Override

View File

@ -2079,7 +2079,16 @@ 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) {
@ -2346,14 +2355,13 @@ public abstract class Loader {
protected List list(
final SessionImplementor session,
final QueryParameters queryParameters,
final Set querySpaces,
final Type[] resultTypes) throws HibernateException {
final Set querySpaces) throws HibernateException {
final boolean cacheable = factory.getSettings().isQueryCacheEnabled() &&
queryParameters.isCacheable();
if ( cacheable ) {
return listUsingQueryCache( session, queryParameters, querySpaces, resultTypes );
return listUsingQueryCache( session, queryParameters, querySpaces );
}
else {
return listIgnoreQueryCache( session, queryParameters );
@ -2367,8 +2375,7 @@ public abstract class Loader {
private List listUsingQueryCache(
final SessionImplementor session,
final QueryParameters queryParameters,
final Set querySpaces,
final Type[] resultTypes) {
final Set querySpaces) {
QueryCache queryCache = factory.getQueryCache( queryParameters.getCacheRegion() );
@ -2384,7 +2391,6 @@ public abstract class Loader {
session,
queryParameters,
querySpaces,
resultTypes,
queryCache,
key
);
@ -2395,7 +2401,6 @@ public abstract class Loader {
putResultInQueryCache(
session,
queryParameters,
resultTypes,
queryCache,
key,
result
@ -2445,17 +2450,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() &&
resultTypes.length == 1 &&
resultTypes[0].isEntityType() &&
getEntityPersister( EntityType.class.cast( resultTypes[0] ) )
returnTypes.length == 1 &&
returnTypes[0].isEntityType() &&
getEntityPersister( EntityType.class.cast( returnTypes[0] ) )
.getEntityMetamodel()
.hasImmutableNaturalId();
@ -2474,7 +2479,7 @@ public abstract class Loader {
try {
result = queryCache.get(
key,
key.getResultTransformer().getCachedResultTypes( resultTypes ),
key.getResultTransformer().getCachedResultTypes( returnTypes ),
isImmutableNaturalKeyLookup,
querySpaces,
session
@ -2506,14 +2511,13 @@ public abstract class Loader {
private 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( resultTypes ),
key.getResultTransformer().getCachedResultTypes( getReturnTypes() ),
result,
queryParameters.isNaturalKeyLookup(),
session
@ -2602,7 +2606,6 @@ public abstract class Loader {
*/
protected ScrollableResults scroll(
final QueryParameters queryParameters,
final Type[] returnTypes,
final HolderInstantiator holderInstantiator,
final SessionImplementor session) throws HibernateException {
@ -2635,7 +2638,7 @@ public abstract class Loader {
session,
this,
queryParameters,
returnTypes,
getReturnTypes(),
holderInstantiator
);
}
@ -2646,7 +2649,7 @@ public abstract class Loader {
session,
this,
queryParameters,
returnTypes,
getReturnTypes(),
holderInstantiator
);
}

View File

@ -112,16 +112,20 @@ 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, resultTypes, null, session);
return scroll(qp, null, session);
}
public List list(SessionImplementor session)
throws HibernateException {
return list( session, translator.getQueryParameters(), querySpaces, resultTypes );
return list( session, translator.getQueryParameters(), querySpaces );
}

View File

@ -333,9 +333,13 @@ 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, resultTypes );
return list( session, queryParameters, querySpaces );
}
@Override
@ -372,7 +376,6 @@ public class CustomLoader extends Loader {
final SessionImplementor session) throws HibernateException {
return scroll(
queryParameters,
resultTypes,
getHolderInstantiator( queryParameters.getResultTransformer(), getReturnAliasesForTransformer() ),
session
);

View File

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

View File

@ -24,6 +24,7 @@
package org.hibernate.test.querycache;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
@ -33,6 +34,7 @@ import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
@ -480,6 +482,50 @@ public class QueryCacheTest extends BaseCoreFunctionalTestCase {
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-3051" )
public void testScalarSQLQuery() {
sessionFactory().getCache().evictQueryRegions();
sessionFactory().getStatistics().clear();
Session s = openSession();
s.beginTransaction();
Item item = new Item();
item.setName("fooName");
item.setDescription("fooDescription");
s.persist(item);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
// Note: StandardQueryCache#put handles single results and multiple results differently. So, test both
// 1 and 2+ scalars.
String sqlQuery = "select name, description from Items";
SQLQuery query = s.createSQLQuery(sqlQuery);
query.setCacheable(true);
query.addScalar("name");
query.addScalar("description");
Object[] result1 = (Object[]) query.uniqueResult();
assertNotNull( result1 );
assertEquals( result1.length, 2 );
assertEquals( result1[0], "fooName" );
assertEquals( result1[1], "fooDescription" );
sqlQuery = "select name from Items";
query = s.createSQLQuery(sqlQuery);
query.setCacheable(true);
query.addScalar("name");
String result2 = (String) query.uniqueResult();
assertNotNull( result2 );
assertEquals( result2, "fooName" );
s.getTransaction().commit();
s.close();
}
// @Test
// public void testGetByCompositeIdNoCache() {
// Query query = em.createQuery("FROM EntityWithCompositeKey e WHERE e.pk = :pk");