HHH-3051 corrected Loader returnTypes
This commit is contained in:
parent
17634bc9e5
commit
8c3db4b76f
|
@ -936,7 +936,7 @@ public class QueryTranslatorImpl extends BasicLoader implements FilterTranslator
|
||||||
@Override
|
@Override
|
||||||
public List list(SessionImplementor session, QueryParameters queryParameters)
|
public List list(SessionImplementor session, QueryParameters queryParameters)
|
||||||
throws HibernateException {
|
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(
|
HolderInstantiator hi = HolderInstantiator.createClassicHolderInstantiator(
|
||||||
holderConstructor, queryParameters.getResultTransformer()
|
holderConstructor, queryParameters.getResultTransformer()
|
||||||
);
|
);
|
||||||
return scroll( queryParameters, returnTypes, hi, session );
|
return scroll( queryParameters, hi, session );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -2082,7 +2082,16 @@ 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) {
|
||||||
|
@ -2349,14 +2358,13 @@ public abstract class Loader {
|
||||||
protected List list(
|
protected List list(
|
||||||
final SessionImplementor session,
|
final SessionImplementor session,
|
||||||
final QueryParameters queryParameters,
|
final QueryParameters queryParameters,
|
||||||
final Set<Serializable> querySpaces,
|
final Set<Serializable> querySpaces) throws HibernateException {
|
||||||
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, resultTypes );
|
return listUsingQueryCache( session, queryParameters, querySpaces );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return listIgnoreQueryCache( session, queryParameters );
|
return listIgnoreQueryCache( session, queryParameters );
|
||||||
|
@ -2370,8 +2378,7 @@ public abstract class Loader {
|
||||||
private List listUsingQueryCache(
|
private List listUsingQueryCache(
|
||||||
final SessionImplementor session,
|
final SessionImplementor session,
|
||||||
final QueryParameters queryParameters,
|
final QueryParameters queryParameters,
|
||||||
final Set<Serializable> querySpaces,
|
final Set<Serializable> querySpaces) {
|
||||||
final Type[] resultTypes) {
|
|
||||||
|
|
||||||
QueryCache queryCache = factory.getQueryCache( queryParameters.getCacheRegion() );
|
QueryCache queryCache = factory.getQueryCache( queryParameters.getCacheRegion() );
|
||||||
|
|
||||||
|
@ -2387,7 +2394,6 @@ public abstract class Loader {
|
||||||
session,
|
session,
|
||||||
queryParameters,
|
queryParameters,
|
||||||
querySpaces,
|
querySpaces,
|
||||||
resultTypes,
|
|
||||||
queryCache,
|
queryCache,
|
||||||
key
|
key
|
||||||
);
|
);
|
||||||
|
@ -2398,7 +2404,6 @@ public abstract class Loader {
|
||||||
putResultInQueryCache(
|
putResultInQueryCache(
|
||||||
session,
|
session,
|
||||||
queryParameters,
|
queryParameters,
|
||||||
resultTypes,
|
|
||||||
queryCache,
|
queryCache,
|
||||||
key,
|
key,
|
||||||
result
|
result
|
||||||
|
@ -2448,17 +2453,17 @@ public abstract class Loader {
|
||||||
final SessionImplementor session,
|
final SessionImplementor session,
|
||||||
final QueryParameters queryParameters,
|
final QueryParameters queryParameters,
|
||||||
final Set<Serializable> querySpaces,
|
final Set<Serializable> 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() &&
|
||||||
resultTypes.length == 1 &&
|
returnTypes.length == 1 &&
|
||||||
resultTypes[0].isEntityType() &&
|
returnTypes[0].isEntityType() &&
|
||||||
getEntityPersister( EntityType.class.cast( resultTypes[0] ) )
|
getEntityPersister( EntityType.class.cast( returnTypes[0] ) )
|
||||||
.getEntityMetamodel()
|
.getEntityMetamodel()
|
||||||
.hasImmutableNaturalId();
|
.hasImmutableNaturalId();
|
||||||
|
|
||||||
|
@ -2477,7 +2482,7 @@ public abstract class Loader {
|
||||||
try {
|
try {
|
||||||
result = queryCache.get(
|
result = queryCache.get(
|
||||||
key,
|
key,
|
||||||
key.getResultTransformer().getCachedResultTypes( resultTypes ),
|
key.getResultTransformer().getCachedResultTypes( returnTypes ),
|
||||||
isImmutableNaturalKeyLookup,
|
isImmutableNaturalKeyLookup,
|
||||||
querySpaces,
|
querySpaces,
|
||||||
session
|
session
|
||||||
|
@ -2509,14 +2514,13 @@ public abstract class Loader {
|
||||||
private void putResultInQueryCache(
|
private 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( resultTypes ),
|
key.getResultTransformer().getCachedResultTypes( getReturnTypes() ),
|
||||||
result,
|
result,
|
||||||
queryParameters.isNaturalKeyLookup(),
|
queryParameters.isNaturalKeyLookup(),
|
||||||
session
|
session
|
||||||
|
@ -2605,7 +2609,6 @@ 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 +2641,7 @@ public abstract class Loader {
|
||||||
session,
|
session,
|
||||||
this,
|
this,
|
||||||
queryParameters,
|
queryParameters,
|
||||||
returnTypes,
|
getReturnTypes(),
|
||||||
holderInstantiator
|
holderInstantiator
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -2649,7 +2652,7 @@ public abstract class Loader {
|
||||||
session,
|
session,
|
||||||
this,
|
this,
|
||||||
queryParameters,
|
queryParameters,
|
||||||
returnTypes,
|
getReturnTypes(),
|
||||||
holderInstantiator
|
holderInstantiator
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,16 +114,20 @@ 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, resultTypes, null, session);
|
return scroll(qp, null, session);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List list(SessionImplementor session)
|
public List list(SessionImplementor session)
|
||||||
throws HibernateException {
|
throws HibernateException {
|
||||||
return list( session, translator.getQueryParameters(), querySpaces, resultTypes );
|
return list( session, translator.getQueryParameters(), querySpaces );
|
||||||
|
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -346,9 +346,13 @@ 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, resultTypes );
|
return list( session, queryParameters, querySpaces );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -384,7 +388,6 @@ public class CustomLoader extends Loader {
|
||||||
throws HibernateException {
|
throws HibernateException {
|
||||||
return scroll(
|
return scroll(
|
||||||
queryParameters,
|
queryParameters,
|
||||||
resultTypes,
|
|
||||||
getHolderInstantiator( queryParameters.getResultTransformer(), getReturnAliasesForTransformer() ),
|
getHolderInstantiator( queryParameters.getResultTransformer(), getReturnAliasesForTransformer() ),
|
||||||
session
|
session
|
||||||
);
|
);
|
||||||
|
|
|
@ -493,7 +493,11 @@ 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(), queryReturnTypes );
|
return list( session, queryParameters, queryTranslator.getQuerySpaces() );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Type[] getReturnTypes() {
|
||||||
|
return queryReturnTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkQuery(QueryParameters queryParameters) {
|
private void checkQuery(QueryParameters queryParameters) {
|
||||||
|
@ -557,7 +561,6 @@ public class QueryLoader extends BasicLoader {
|
||||||
checkQuery( queryParameters );
|
checkQuery( queryParameters );
|
||||||
return scroll(
|
return scroll(
|
||||||
queryParameters,
|
queryParameters,
|
||||||
queryReturnTypes,
|
|
||||||
buildHolderInstantiator( queryParameters.getResultTransformer() ),
|
buildHolderInstantiator( queryParameters.getResultTransformer() ),
|
||||||
session
|
session
|
||||||
);
|
);
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
package org.hibernate.test.querycache;
|
package org.hibernate.test.querycache;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -33,6 +34,7 @@ import java.util.Map;
|
||||||
import org.hibernate.Criteria;
|
import org.hibernate.Criteria;
|
||||||
import org.hibernate.Hibernate;
|
import org.hibernate.Hibernate;
|
||||||
import org.hibernate.Query;
|
import org.hibernate.Query;
|
||||||
|
import org.hibernate.SQLQuery;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.Transaction;
|
import org.hibernate.Transaction;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
|
@ -487,6 +489,50 @@ public class QueryCacheTest extends BaseCoreFunctionalTestCase {
|
||||||
s.close();
|
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
|
// @Test
|
||||||
// public void testGetByCompositeIdNoCache() {
|
// public void testGetByCompositeIdNoCache() {
|
||||||
// Query query = em.createQuery("FROM EntityWithCompositeKey e WHERE e.pk = :pk");
|
// Query query = em.createQuery("FROM EntityWithCompositeKey e WHERE e.pk = :pk");
|
||||||
|
|
Loading…
Reference in New Issue