使列索引超出范围,0 < 1

时间:2012-07-04 15:12:17

标签: openjpa

我正在使用OpenJPA 2.2.0,当我执行select语句时,我收到“列索引超出范围,0 <1”错误。

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());
}

当query.getResultList()执行时,我得到“列索引超出范围,0 <1”错误。有人能让我知道上面的代码有什么问题吗?

@UPDATE

当我在控制台上打印堆栈后没有捕获异常时。

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 在搜索和调试之后,发现persistence.xml文件指向一个旧数据库,因此它给出了上述错误。

更改了数据库URL,应用程序运行正常。 :)

3 个答案:

答案 0 :(得分:0)

根本不确定,但您似乎使用了持久化上下文(因为您使用了clear方法)。你有没有尝试使用

createQuery

方法而不是

createNativeQuery

一个?它将使用JPA持久化上下文,它适用于我的Java EE项目。 祝你好运。

答案 1 :(得分:0)

似乎jdbc中的列索引是从1计算得出的。它不同于从0开始计算的数组。

答案 2 :(得分:-1)

我也有这个问题。你可以改变

companyUserDOObj = query.getResultList();

companyUserDOObj.addAll(query.getResultList());