java - OpenJPA 查询第二次失败(可能是因为字节数组参数)

标签 java db2 websphere openjpa

我有以下 OpenJPA 查询(通过 db2 数据库):

Select number from Number number where number.uuid = :uuid

列 uuid 定义为 VARCHAR FOR BIT DATA(16)。在实体中它表示为 byte[] 类型字段。

问题是,当我第一次使用该查询调用方法时(WebSphere 服务器启动后),它工作正常。后续调用会导致错误(我什至启用了跟踪日志以查看错误所在)

1st time:> query is running, everything's ok
[11/25/15 13:27:03:803 IST] 0000001d Query         3   openjpa.Query: Trace: Executing query: [Select n from Number n where n.uuid = :uuid] with parameters: ?
[11/25/15 13:27:04:199 IST] 0000001d jdbc_SQL      3   openjpa.jdbc.SQL: Trace: <t 507109353, conn 1755254015> executing prepstmnt -1123134307 SELECT t0.NUMBER_ID, t0.comment, t0.EMPLOYEE_ID, t0.number, t0.RANGE_ID, t0.status, t0.uuid FROM PHONES.Number t0 WHERE (t0.uuid = ?)  [params=?]
[11/25/15 13:27:04:249 IST] 0000001d jdbc_SQL      3   openjpa.jdbc.SQL: Trace: <t 507109353, conn 1755254015> [50 ms] spent
[11/25/15 13:27:04:251 IST] 0000001d jdbc_JDBC     3   openjpa.jdbc.JDBC: Trace: <t 507109353, conn 1755254015> [0 ms] close
2nd time:>
[11/25/15 13:27:09:068 IST] 0000001d Query         3   openjpa.Query: Trace: Executing query: [Select n from Number n where n.uuid = :uuid] with parameters: ?
[11/25/15 13:27:09:556 IST] 0000001d BusinessExcep E   CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "approve" on bean "BeanId(PhoneAllocationEAR#PhoneAllocationEJB.jar#RangeProvider, null)". Exception data: <openjpa-2.1.2-SNAPSHOT-r422266:1548248 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Failed to execute query "Select n from Number n where n.uuid = :uuid". Check the query syntax for correctness. See nested exception for details.

这是嵌套异常:

Caused by: java.lang.NullPointerException
at org.apache.openjpa.jdbc.sql.DB2Dictionary.setBytes(DB2Dictionary.java:1037)
at org.apache.openjpa.jdbc.sql.DBDictionary.setUnknown(DBDictionary.java:1481)
at org.apache.openjpa.jdbc.sql.DBDictionary.setUnknown(DBDictionary.java:1429)
at org.apache.openjpa.jdbc.kernel.PreparedSQLStoreQuery$PreparedSQLExecutor.executeQuery(PreparedSQLStoreQuery.java:114)
at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1005)
at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:863)
... 86 more 

这是出现异常的代码:

public String findByUUID(UUID uuid) {

    TypedQuery<Number> query = em.createQuery("Select n from Number n where n.uuid = :uuid", Number.class);
    ByteBuffer bb = ByteBuffer.allocate(16);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    byte[] parameter = bb.array();

    query.setParameter("uuid", parameter);

    List<Number> result = new ArrayList<Number>(query.getResultList());
    return result;
}

*附加信息: 如果我从列描述中删除位数据 - 我会收到另一个错误:

nested exception is: javax.ejb.EJBException: See nested exception; nested exception is: org.apache.openjpa.persistence.PersistenceException: The value of input variable, expression or parameter number "1" cannot be used because of its data type.. SQLCODE=-301, SQLSTATE=07006, DRIVER=4.14.113 {prepstmnt 2039708348 SELECT t0.NUMBER_ID, t0.comment, t0.EMPLOYEE_ID, t0.number, t0.RANGE_ID, t0.status, t0.uuid FROM PHONES.Number t0 WHERE (t0.uuid = ?) [params=?]} [code=-301, state=07006]SQLCA OUTPUT[Errp=SQLRI4A4, Errd=-2145779603, 0, 0, 0, -3700, 0] The value of input variable, expression or parameter number "1" cannot be used because of its data type.. SQLCODE=-301, SQLSTATE=07006, DRIVER=4.14.113 FailedObject: Select n from Number n where n.uuid = :uuid [java.lang.String]

我是通过查看 openJPA 的来源了解到的。但这同时对我没有帮助。

最佳答案

尝试禁用准备好的查询缓存。众所周知,它有很多问题。如果需要,您可以搜索 OpenJPA JIRA 以查找详细信息。

<property name="openjpa.jdbc.QuerySQLCache" value="true(EnableStatistics=true)">

关于java - OpenJPA 查询第二次失败(可能是因为字节数组参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33915801/

相关文章:

java - Hadoop 追加到 Sequencefile

java - 在 DB2 中调用 Java 存储过程时出现错误 -4304

java - 控制台无输出

database - 使用 DB2 .NET 数据提供程序的异步过程调用

java - Tomcat 8 中 DB2 的 Log4j jdbc appender

java - 无法在 JAX-WS Eclipse 生成的客户端上设置自定义超时

java - WAS 7.0.0.23 - MDB 中带有激活规范的默认拦截器会抛出 classcastException

java - 线程池并在计数器达到 X 时停止执行 for 循环

java - 如何在弹出软键盘时使聚焦的 EditText 可见?

java - 无法使用 Hibernate 5 部署应用程序