-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate DB2 createNativeQuery adds extra column to result
PostPosted: Thu Nov 04, 2010 7:12 am 
Newbie

Joined: Thu Feb 12, 2009 10:00 am
Posts: 18
When using Hibernate and DB2 I am getting an extra column returned in the result array. The following code should return an array of X elements, however with DB2 I am getting X+1 elements.

Code:
Query query = m_entityManager.createNativeQuery(sqlQuery);
query.setParameter(1, someId);
query.setMaxResults(pageSize);
query.setFirstResult(pageSize * pageIndex);
List results = query.getResultList();
for (Object resultObj : results) {
Object[] result = (Object[]) resultObj;
}


Debugging this showed that the first element in the array contains a BigInteger with a value of 1. The second element in the result array contains the value of the first column from the database. Running the SQL statement direct against the database does not demonstrate this issue.

I am guessing this is some sort of row identifier which is added by Hibernate to handle the paging. What I don't understand is why it is not added for other databases.

Appreciate any insight into this issue.
Thanks in advance
Alex


Top
 Profile  
 
 Post subject: Re: Hibernate DB2 createNativeQuery adds extra column to result
PostPosted: Thu Nov 04, 2010 6:54 pm 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
Show SQL in question.


Top
 Profile  
 
 Post subject: Re: Hibernate DB2 createNativeQuery adds extra column to result
PostPosted: Fri Nov 05, 2010 6:33 am 
Newbie

Joined: Thu Feb 12, 2009 10:00 am
Posts: 18
This is the sql statement used:

Quote:
select a.ID, a.SORTKEY1, a.SORTKEY2, a.SORTKEY3, b.USERKEY1, COUNTS.ENVPAGECOUNT, COUNTS.CJCOUNT, a.TRANSACTIONNO
FROM TABLEA a, TABLEB b, TABLEC c, (select envID, SUM(c.PAGECOUNT) ENVPAGECOUNT, COUNT(c.ID) CJCOUNT FROM TABLEC c GROUP BY c.envID) COUNTS
WHERE COUNTS.envID = a.ID AND a.mcjId = c.ID AND c.DOCID = b.ID AND a.SPOOLID = ? AND a.STEPNO < 10 ORDER BY a.ID ASC


Thanks for getting back to me on this.


Top
 Profile  
 
 Post subject: Re: Hibernate DB2 createNativeQuery adds extra column to result
PostPosted: Fri Nov 05, 2010 11:22 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
Wow, you are right. The hibernate DB2 dialect really does it.

Here is the code from "org.hibernate.dialect.DB2Dialect".

Code:
   private String getRowNumber(String sql) {
      StringBuffer rownumber = new StringBuffer(50)
         .append("rownumber() over(");

      int orderByIndex = sql.toLowerCase().indexOf("order by");

      if ( orderByIndex>0 && !hasDistinct(sql) ) {
         rownumber.append( sql.substring(orderByIndex) );
      }

      rownumber.append(") as rownumber_,");

      return rownumber.toString();
   }

   public String getLimitString(String sql, boolean hasOffset) {

      int startOfSelect = sql.toLowerCase().indexOf("select");

      StringBuffer pagingSelect = new StringBuffer( sql.length()+100 )
               .append( sql.substring(0, startOfSelect) ) //add the comment
               .append("select * from ( select ") //nest the main query in an outer select
               .append( getRowNumber(sql) ); //add the rownnumber bit into the outer query select list

      if ( hasDistinct(sql) ) {
         pagingSelect.append(" row_.* from ( ") //add another (inner) nested select
            .append( sql.substring(startOfSelect) ) //add the main query
            .append(" ) as row_"); //close off the inner nested select
      }
      else {
         pagingSelect.append( sql.substring( startOfSelect + 6 ) ); //add the main query
      }

      pagingSelect.append(" ) as temp_ where rownumber_ ");

      //add the restriction to the outer select
      if (hasOffset) {
         pagingSelect.append("between ?+1 and ?");
      }
      else {
         pagingSelect.append("<= ?");
      }

      return pagingSelect.toString();
   }


I guess other databases have the thing handled differently in their dialect classes.


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

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.