use of org.hibernate.transform.ResultTransformer in project hibernate-orm by hibernate.
the class QueryKeyTest method doResultTransformerTest.
// Reproduces HHH-5628; commented out because FailureExpected is not working here...
/*
public void testAliasToBeanConstructorFailureExpected() throws Exception {
// AliasToBeanConstructorResultTransformer is not Serializable because
// java.lang.reflect.Constructor is not Serializable;
doResultTransformerTest(
new AliasToBeanConstructorResultTransformer( AClass.class.getConstructor( String.class ) ), false
);
}
*/
private void doResultTransformerTest(ResultTransformer transformer, boolean isSingleton) {
Map transformerMap = new HashMap();
transformerMap.put(transformer, "");
assert transformerMap.size() == 1 : "really messed up";
Object old = transformerMap.put(transformer, "value");
assert old != null && transformerMap.size() == 1 : "apparent QueryKey equals/hashCode issue";
// finally, lets serialize it and see what happens
ResultTransformer transformer2 = (ResultTransformer) SerializationHelper.clone(transformer);
old = transformerMap.put(transformer2, "new value");
assert old != null && transformerMap.size() == 1 : "deserialization did not set hashCode or equals properly";
if (isSingleton) {
assert transformer == transformer2 : "deserialization issue for singleton transformer";
} else {
assert transformer != transformer2 : "deserialization issue for non-singleton transformer";
}
assert transformer.equals(transformer2) : "deep copy issue";
}
use of org.hibernate.transform.ResultTransformer in project hibernate-orm by hibernate.
the class Loader method listUsingQueryCache.
private List listUsingQueryCache(final SharedSessionContractImplementor session, final QueryParameters queryParameters, final Set<Serializable> querySpaces, final Type[] resultTypes) {
QueryResultsCache queryCache = factory.getCache().getQueryResultsCache(queryParameters.getCacheRegion());
QueryKey key = generateQueryKey(session, queryParameters);
if (querySpaces == null || querySpaces.size() == 0) {
LOG.tracev("Unexpected querySpaces is {0}", (querySpaces == null ? querySpaces : "empty"));
} else {
LOG.tracev("querySpaces is {0}", querySpaces);
}
List result = getResultFromQueryCache(session, queryParameters, querySpaces, resultTypes, queryCache, key);
if (result == null) {
result = doList(session, queryParameters, key.getResultTransformer());
putResultInQueryCache(session, queryParameters, resultTypes, queryCache, key, result);
}
ResultTransformer resolvedTransformer = resolveResultTransformer(queryParameters.getResultTransformer());
if (resolvedTransformer != null) {
result = (areResultSetRowsTransformedImmediately() ? key.getResultTransformer().retransformResults(result, getResultRowAliases(), queryParameters.getResultTransformer(), includeInResultRow()) : key.getResultTransformer().untransformToTuples(result));
}
return getResultList(result, queryParameters.getResultTransformer());
}
use of org.hibernate.transform.ResultTransformer in project hibernate-orm by hibernate.
the class CustomLoader method scroll.
public ScrollableResultsImplementor scroll(final QueryParameters queryParameters, final SharedSessionContractImplementor session) throws HibernateException {
ResultTransformer resultTransformer = queryParameters.getResultTransformer();
HolderInstantiator holderInstantiator = (resultTransformer == null) ? HolderInstantiator.NOOP_INSTANTIATOR : new HolderInstantiator(resultTransformer, this::getReturnAliasesForTransformer);
return scroll(queryParameters, resultTypes, holderInstantiator, session);
}
use of org.hibernate.transform.ResultTransformer in project candlepin by candlepin.
the class AbstractHibernateCurator method listByCriteria.
@SuppressWarnings("unchecked")
@Transactional
public Page<List<E>> listByCriteria(Criteria c, PageRequest pageRequest) {
Page<List<E>> page = new Page<>();
if (pageRequest != null) {
// see https://forum.hibernate.org/viewtopic.php?t=974802
// Save original Projection and ResultTransformer
CriteriaImpl cImpl = (CriteriaImpl) c;
Projection origProjection = cImpl.getProjection();
ResultTransformer origRt = cImpl.getResultTransformer();
// Get total number of records by setting a rowCount projection
page.setMaxRecords(findRowCount(c));
// Restore original Projection and ResultTransformer
c.setProjection(origProjection);
c.setResultTransformer(origRt);
page.setPageData(loadPageData(c, pageRequest));
page.setPageRequest(pageRequest);
} else {
List<E> pageData = listByCriteria(c);
page.setMaxRecords(pageData.size());
page.setPageData(pageData);
}
return page;
}
use of org.hibernate.transform.ResultTransformer in project ngtesting-platform by aaronchen2k.
the class BaseDaoImpl method findPage.
@Override
public Page findPage(DetachedCriteria dc, int start, int limit) {
Criteria c = dc.getExecutableCriteria(getSession());
CriteriaImpl impl = (CriteriaImpl) c;
Projection projection = impl.getProjection();
ResultTransformer transformer = impl.getResultTransformer();
List<CriteriaImpl.OrderEntry> orderEntries = (List<CriteriaImpl.OrderEntry>) ReflectionUtils.getFieldValue(impl, "orderEntries");
ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList<CriteriaImpl.OrderEntry>());
// 执行Count查询
c.setResultTransformer(CriteriaImpl.DISTINCT_ROOT_ENTITY);
long total = (Long) c.setProjection(Projections.countDistinct("id")).uniqueResult();
// 将之前的Projection和OrderBy条件重新设回去
c.setProjection(projection);
c.setResultTransformer(transformer);
ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
c.setFirstResult(start);
c.setMaxResults(limit);
List list = c.list();
return new Page(start, limit, Integer.parseInt(String.valueOf(total)), (list == null ? new ArrayList() : list));
}
Aggregations