0

我正在使用 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”错误。有人可以让我知道上面的代码有什么问题吗?

@更新

当我没有捕捉到异常时,控制台上会打印以下堆栈。

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,应用程序运行正常。:)

4

3 回答 3

0

完全不确定,但您似乎使用了持久性上下文(因为您使用 clear 方法)。您是否尝试使用

createQuery

方法而不是

createNativeQuery

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

于 2012-07-04T15:41:34.773 回答
0

jdbc 中的列索引似乎是从 1 计算的。它与从 0 计算的数组不同。

于 2018-12-12T02:51:15.330 回答
-1

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

companyUserDOObj = query.getResultList();

companyUserDOObj.addAll(query.getResultList()); 
于 2015-06-30T21:43:44.267 回答