问题
I am using OpenJPA 2.2.0 and when I am executing a select statement I am getting "Column index out of range, 0 < 1" error.
EntityManager entityMgrObj = emf.getEntityManager();
entityMgrObj.clear();
Query query = entityMgrObj.createNativeQuery("select * from company_user where user_id = 1001);
List<CompanyUserDO> companyUserDOObj = null;
try {
companyUserDOObj = query.getResultList();
} catch (Exception e) {
System.out.println(e.getMessage());
}
When the query.getResultList() executes I get "Column index out of range, 0 < 1" error. Can somebody let me know what is wrong in the above code?
@UPDATE
When I do not catch the exception following stack is printed on the console.
Exception in thread "main" <openjpa-2.2.0-r422266:1244990 fatal general error> org.apache.openjpa.persistence.PersistenceException: Column Index out of range, 0 < 1.
FailedObject: select * from company_user where user_id=1001 [java.lang.String]
at org.apache.openjpa.jdbc.sql.DBDictionary.narrow(DBDictionary.java:4918)
at org.apache.openjpa.jdbc.sql.DBDictionary.newStoreException(DBDictionary.java:4878)
at org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:136)
at org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:118)
at org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:70)
at org.apache.openjpa.jdbc.kernel.GenericResultObjectProvider.handleCheckedException(GenericResultObjectProvider.java:125)
at org.apache.openjpa.lib.rop.EagerResultList.<init>(EagerResultList.java:40)
at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1251)
at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1007)
at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:863)
at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:794)
at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:542)
at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:286)
at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:302)
at zumigo.geofence.test.dalTest.main(dalTest.java:62)
Caused by: java.sql.SQLException: Column Index out of range, 0 < 1.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ResultSetImpl.checkColumnBounds(ResultSetImpl.java:812)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5651)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5570)
at org.apache.commons.dbcp.DelegatingResultSet.getString(DelegatingResultSet.java:213)
at org.apache.commons.dbcp.DelegatingResultSet.getString(DelegatingResultSet.java:213)
at org.apache.openjpa.lib.jdbc.DelegatingResultSet.getString(DelegatingResultSet.java:121)
at org.apache.openjpa.jdbc.sql.DBDictionary.getString(DBDictionary.java:886)
at org.apache.openjpa.jdbc.sql.ResultSetResult.getStringInternal(ResultSetResult.java:474)
at org.apache.openjpa.jdbc.sql.AbstractResult.getString(AbstractResult.java:767)
at org.apache.openjpa.jdbc.meta.strats.StringFieldStrategy.getPrimaryKeyValue(StringFieldStrategy.java:217)
at org.apache.openjpa.jdbc.meta.ClassMapping.getObjectId(ClassMapping.java:188)
at org.apache.openjpa.jdbc.meta.ClassMapping.getObjectId(ClassMapping.java:147)
at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:1002)
at org.apache.openjpa.jdbc.kernel.GenericResultObjectProvider.getResultObject(GenericResultObjectProvider.java:93)
at org.apache.openjpa.lib.rop.EagerResultList.<init>(EagerResultList.java:36)
... 8 more
@UPDATE 2 After searching and debugging it was found that the persistence.xml file was pointing to an old database hence it was giving the above mentioned error.
Changed the database URL and the application is working fine. :)
回答1:
Not sure at all, but you seem to use a persistence context (since you use the clear method). Did you try to use the
createQuery
method instead of the
createNativeQuery
one? It'll use the JPA persistence context, it works on my Java EE project. Good luck.
回答2:
It seems like that column index in jdbc is computed from 1. And it's different from array, which is computed from 0.
回答3:
I also had that problem. You can change
companyUserDOObj = query.getResultList();
to
companyUserDOObj.addAll(query.getResultList());
来源:https://stackoverflow.com/questions/11331712/getting-column-index-out-of-range-0-1