-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem with AliasToEntityMapResultTransformer
PostPosted: Thu Aug 15, 2013 5:15 am 
Newbie

Joined: Thu Aug 15, 2013 5:04 am
Posts: 1
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...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.