| Hey guys, Just had a fancy ride with AliasToBeanResultTransformer, that's how it fold out: I was trying to group by my result with HQL as follows:
SELECT deviceId, itemType, COUNT(*) as count FROM FavoriteItem WHERE userId = :userId and expireTime > :now GROUP BY deviceId, itemType
It was accompanied by a Java bean defined below:
public static class GroupCount {
private String deviceId;
private String itemType;
private int count;
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(final String deviceId) {
this.deviceId = deviceId;
}
public String getItemType() {
return itemType;
}
public void setItemType(final String itemType) {
this.itemType = itemType;
}
public long getCount() {
return count;
}
public void setCount(final int count) {
this.count = count;
}
}
An exception is thrown after executing this hql:
Caused by: org.hibernate.PropertySetterAccessException: IllegalArgumentException occurred while calling setter for property [com.eques.eqhome.db.dao.FavoriteItemDao$GroupCount.count (expected type = int)]; target = [com.eques.eqhome.db.dao.FavoriteItemDao$GroupCount@5a1cacf8], property value = [3] setter of com.eques.eqhome.db.dao.FavoriteItemDao$GroupCount.count
at org.hibernate.property.access.spi.SetterMethodImpl.set(SetterMethodImpl.java:99) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:78) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.hql.internal.HolderInstantiator.instantiate(HolderInstantiator.java:85) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.loader.hql.QueryLoader.getResultList(QueryLoader.java:470) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.loader.Loader.list(Loader.java:2502) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:384) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1490) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1445) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
... 112 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_91]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
at org.hibernate.property.access.spi.SetterMethodImpl.set(SetterMethodImpl.java:44) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:78) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.hql.internal.HolderInstantiator.instantiate(HolderInstantiator.java:85) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.loader.hql.QueryLoader.getResultList(QueryLoader.java:470) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.loader.Loader.list(Loader.java:2502) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:384) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1490) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1445) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
It's obvious to see the type of count is mismatched, but still this exception failed to reveal the actual type of count, I spent several times to figure it out (yea, break-point!) Can someone just make it more clear ? Thanks in advance! and have a nice day! |