Attempting to use CreateSQLQuery with SQL code beginning with capitalized SELECT, throws StringIndexOutOfBoundsException.
In the code for the method:
public String getLimitString(String sql, boolean hasOffset) {
int startOfSelect = sql.indexOf("select");
StringBuffer pagingSelect = new StringBuffer( sql.length()+100 )
.append( sql.substring(0, startOfSelect) ) //add the comment
//nest the main query in an outer select
.append("select * from ( select ")
//add the rownnumber bit into the outer query select list
.append( getRowNumber(sql) );
(comments adjusted for readability)
Note that it is attempting to find the index of the word "select" in lowercase (mine was upppercase), which returns -1 or not found. Then when it attempts to append the substring(0, startOfSelect), it throws the StringIndexOutOfBoundsException because StartPoint (0) is greater than EndPoint (-1).
Knowing the source code, the work around is obvious, use lower case.
Hibernate version:
2.1.7c
Header from src file:
DB2Dialect.java,v 1.15 2004/08/13 06:39:27 oneovthafew
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java(Compiled Code))
at net.sf.hibernate.dialect.DB2Dialect.getLimitString(DB2Dialect.java:187)
at net.sf.hibernate.dialect.Dialect.getLimitString(Dialect.java:460)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:777)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:265)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
at net.sf.hibernate.loader.SQLLoader.list(SQLLoader.java:92)
at net.sf.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:3855)
at net.sf.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:52)
at com.ibm.services.learning.kcenter.data.dao.SearchDaoImpl$1.doInHibernate(SearchDaoImpl.java:171)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:312)
at org.springframework.orm.hibernate.HibernateTemplate.executeFind(HibernateTemplate.java:293)
at com.ibm.services.learning.kcenter.data.dao.SearchDaoImpl.queryHelper(SearchDaoImpl.java:164)
at com.ibm.services.learning.kcenter.data.dao.SearchDaoImpl.getPopularContentList(SearchDaoImpl.java:146)
at com.ibm.services.kcenter.data.dao.SearchDaoTest.testGetPopularContentListSort5(SearchDaoTest.java:598)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:58)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:60)
at java.lang.reflect.Method.invoke(Method.java:391)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
Name and version of the database you are using:
DB2 8.3 fp7
The generated SQL (show_sql=true):
My sql for CreateSQLQuery method:
SELECT {c.*}, {ct.*}
FROM ...
WHERE ...
ORDER BY ...
2005-06-09 08:34:42,395 [main] DEBUG com.ibm.services.learning.kcenter.data.dao.SearchDaoImpl -
SELECT {c.*}, {ct.*} from ContentDO c, ContentTypeDO ct, ContentCategoryDO cc, RoleAccessDO ra, UserDO u
where c.contentId = cc.contentDO.contentId
and cc.categoryDO.categoryId in (2, 4, 5, 3, 6, 7)
and cc.categoryDO.categoryId = ra.categoryDO.categoryId
and u.userId = 1
and ra.roleDO.roleId = u.roleDO.roleId
and cc.accessLevel > 0
and cc.accessLevel <= ra.accessLevel
and c.localeDO.localeId = u.localeDO.localeId
and c.status = 'A'
and c.typeId = ct.typeId
and c.typeId = 1
order by ct.shortDescription, c.title
Debug level Hibernate log excerpt:
John M. Evans
IBM
|