Hi,
I have a method where I execute some native sql queries which I process in a scrollable result.
For some I have entities defined, which works fine.
But some queries are more generic and I want to process them with the AliasToEntityMapResultTransformer to get a map with the values of each row. With this transformer I get an ArrayIndexOutOfBoundException because the aliases are not set. I have no clue why, because with an entity it works, which as far as I know also uses the aliases for mapping....
Any suggestions how I can solve the generic approach? One way would be to also provide the aliases with the query, but I don't like this way, because I always have to maintain the aliases, when I change the sql query....
Code:
protected void processQuery(String sql, Class<?> entity, ExportProcessor processor, String mandantId, String dienstId) {
Session hibernateSession = (Session) entityManager.getDelegate();
boolean opened = false;
if (!hibernateSession.isOpen()) {
hibernateSession = hibernateSession.getSessionFactory().openSession();
opened = true;
}
log.debug("SQL: " + sql);
Query query = hibernateSession.createSQLQuery(sql);
if (entity != null) {
query = ((SQLQuery) query).addEntity(entity);
} else {
query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
}
query.getReturnAliases();
query.setFetchSize(100);
query.setCacheable(false);
query.setCacheMode(CacheMode.IGNORE);
if (sql.contains(":mandantId")) {
query.setParameter("mandantId", mandantId);
}
if (sql.contains(":dienstId")) {
query.setParameter("dienstId", dienstId);
}
ScrollableResults result = query.scroll(ScrollMode.FORWARD_ONLY);
while (result.next()) {
processor.process(result.get(0));
hibernateSession.flush();
hibernateSession.clear();
}
result.close();
if (opened) {
hibernateSession.close();
}
}
Thanks in advance...