I am getting error
aliases expected length is 1; actual length is 4
at org.hibernate.transform.CacheableResultTransformer.transformTuple
I have JPA + Hibernate configurations and query cache and second level cache using Eh-Cache.
Configuration: PostgreSQL 9.6 + JPA 2.1 + Hibernate 5.2.3.Final
I am trying to execute NativeQuery with SqlResultSetMapping [custom result set class]. Everything is working fine when I am disabling cache.
But, getting above error on when I am enabling cache. Cache is working fine except for NativeQuery.
Table SCHEMA:
PK first second third
1 A abc C
2 A abc C
3 A xyz D
4 B abc C
5 B xyz C
6 B abc D
7 A xyz C
8 A abc D
SQL Native QUERY:
SELECT t.first,t.second,
COUNT(t.second) total,
COALESCE(t1.ccount, 0) ccount,
COALESCE(t2.dcount, 0) dcount
FROM test t
LEFT JOIN (SELECT
COUNT(third) AS ccount, FIRST, SECOND
FROM test
WHERE third = 'C'
GROUP BY SECOND,FIRST) t1
ON (t1.first = t.first AND t1.SECOND = t.SECOND)
LEFT JOIN (SELECT
COUNT(third) AS dcount, FIRST, SECOND
FROM test
WHERE third = 'D'
GROUP BY SECOND,FIRST) t2
ON (t2.first = t.first AND t2.SECOND = t.SECOND)
GROUP BY t.SECOND, t.first;
SqlResultSetMapping
@SqlResultSetMapping(name = "RESULT_SET_NAME", classes = {
@ConstructorResult( targetClass = TestResult.class,
columns = { @ColumnResult(name = "first", type = String.class),
@ColumnResult(name = "second", type = String.class),
@ColumnResult(name = "total", type = String.class),
@ColumnResult(name = "ccount", type = String.class),
@ColumnResult(name = "dcount", type = String.class) }) })
query = getEntityManager().createNativeQuery(nativeQuery, "RESULT_SET_NAME");
query.setHint("org.hibernate.cacheable", true);
result = query.getResultList();
Expected result-set
first second total ccount dcount
------ ------ ------ ------ --------
A abc 3 2 1
B abc 2 1 1
A xyz 2 1 1
B xyz 1 1 0
stack trace
aliases expected length is 1; actual length is 4
java.lang.IllegalStateException: aliases expected length is 1; actual length is 4
at org.hibernate.transform.CacheableResultTransformer.transformTuple(CacheableResultTransformer.java:155)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:770)
at org.hibernate.loader.Loader.processResultSet(Loader.java:985)
at org.hibernate.loader.Loader.doQuery(Loader.java:943)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
at org.hibernate.loader.Loader.doList(Loader.java:2615)
at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2460)
at org.hibernate.loader.Loader.list(Loader.java:2422)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:335)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2129)
at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:981)
at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:147)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1398)
at org.hibernate.Query.getResultList(Query.java:417)
./gradlew hibernate-core:test --tests *ConstructorResultNativeQueryTest -Pdb=pgsql
(assuming you've set-up a databasehibernate_orm_test
and a user with the same name and password and full access to it). I can't duplicate your error. – Unsure@Override protected void addConfigOptions(Map s){s.put(org.hibernate.cfg.AvailableSettings.USE_QUERY_CACHE,"true");}
– Unsurejava.io.Serializable
and have a no-args constructor. – Unsure