HHH-3051 corrected Loader returnTypes
This commit is contained in:
parent
fcd6f1b330
commit
2a90123991
|
@ -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(), actualReturnTypes );
|
||||
return list( session, queryParameters, getQuerySpaces() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1172,7 +1172,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
|
||||
|
|
|
@ -2082,7 +2082,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) {
|
||||
|
@ -2349,14 +2358,13 @@ public abstract class Loader {
|
|||
protected List list(
|
||||
final SessionImplementor session,
|
||||
final QueryParameters queryParameters,
|
||||
final Set<Serializable> querySpaces,
|
||||
final Type[] resultTypes) throws HibernateException {
|
||||
final Set<Serializable> 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 );
|
||||
|
@ -2370,8 +2378,7 @@ public abstract class Loader {
|
|||
private List listUsingQueryCache(
|
||||
final SessionImplementor session,
|
||||
final QueryParameters queryParameters,
|
||||
final Set<Serializable> querySpaces,
|
||||
final Type[] resultTypes) {
|
||||
final Set<Serializable> querySpaces) {
|
||||
|
||||
QueryCache queryCache = factory.getQueryCache( queryParameters.getCacheRegion() );
|
||||
|
||||
|
@ -2387,7 +2394,6 @@ public abstract class Loader {
|
|||
session,
|
||||
queryParameters,
|
||||
querySpaces,
|
||||
resultTypes,
|
||||
queryCache,
|
||||
key
|
||||
);
|
||||
|
@ -2398,7 +2404,6 @@ public abstract class Loader {
|
|||
putResultInQueryCache(
|
||||
session,
|
||||
queryParameters,
|
||||
resultTypes,
|
||||
queryCache,
|
||||
key,
|
||||
result
|
||||
|
@ -2448,17 +2453,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() &&
|
||||
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();
|
||||
|
||||
|
@ -2477,7 +2482,7 @@ public abstract class Loader {
|
|||
try {
|
||||
result = queryCache.get(
|
||||
key,
|
||||
key.getResultTransformer().getCachedResultTypes( resultTypes ),
|
||||
key.getResultTransformer().getCachedResultTypes( returnTypes ),
|
||||
isImmutableNaturalKeyLookup,
|
||||
querySpaces,
|
||||
session
|
||||
|
@ -2509,14 +2514,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
|
||||
|
@ -2605,7 +2609,6 @@ public abstract class Loader {
|
|||
*/
|
||||
protected ScrollableResults scroll(
|
||||
final QueryParameters queryParameters,
|
||||
final Type[] returnTypes,
|
||||
final HolderInstantiator holderInstantiator,
|
||||
final SessionImplementor session) throws HibernateException {
|
||||
|
||||
|
@ -2638,7 +2641,7 @@ public abstract class Loader {
|
|||
session,
|
||||
this,
|
||||
queryParameters,
|
||||
returnTypes,
|
||||
getReturnTypes(),
|
||||
holderInstantiator
|
||||
);
|
||||
}
|
||||
|
@ -2649,7 +2652,7 @@ public abstract class Loader {
|
|||
session,
|
||||
this,
|
||||
queryParameters,
|
||||
returnTypes,
|
||||
getReturnTypes(),
|
||||
holderInstantiator
|
||||
);
|
||||
}
|
||||
|
|
|
@ -114,16 +114,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 );
|
||||
|
||||
}
|
||||
@Override
|
||||
|
|
|
@ -346,9 +346,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
|
||||
|
@ -384,7 +388,6 @@ public class CustomLoader extends Loader {
|
|||
throws HibernateException {
|
||||
return scroll(
|
||||
queryParameters,
|
||||
resultTypes,
|
||||
getHolderInstantiator( queryParameters.getResultTransformer(), getReturnAliasesForTransformer() ),
|
||||
session
|
||||
);
|
||||
|
|
|
@ -493,7 +493,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) {
|
||||
|
@ -557,7 +561,6 @@ public class QueryLoader extends BasicLoader {
|
|||
checkQuery( queryParameters );
|
||||
return scroll(
|
||||
queryParameters,
|
||||
queryReturnTypes,
|
||||
buildHolderInstantiator( queryParameters.getResultTransformer() ),
|
||||
session
|
||||
);
|
||||
|
|
|
@ -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;
|
||||
|
@ -487,6 +489,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");
|
||||
|
|
Loading…
Reference in New Issue