(getEntityManager()返回spring注入的EntityManager,database字段history类型为text或varchar2(2000))
```java TypedQuery query = getEntityManager().createQuery( "SELECT h FROM History h WHERE h.entityId = :entityId", History.class); query.setParameter("entityId", entity.getId()); List historyList = query.getResultList(); ```
Query query = getEntityManager().createNativeQuery("insert into table_name(..., history, ....) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
[...]
.setParameter(6, entity.getHistory())
[...]
query.executeUpdate();
抛出了奇怪的异常:
17/11/11 06:26:09:009 [pool-2-thread-1] WARN util.JDBCExceptionReporter:100 - SQL Error: 0, SQLState: 42804
17/11/11 06:26:09:009 [pool-2-thread-1] ERROR util.JDBCExceptionReporter:101 - ERROR: **column "history" is of type text but expression is of type bytea**
提示:您需要重写或转换表达式。问题仅在以下配置中出现:
操作系统:CentOS release 5.6(Final)
Java:1.6.0_26
数据库:PostgreSQL 8.1
JDBC驱动程序:postgresql-9.1-901.jdbc4
应用服务器:apache-tomcat-6.0.28 在其他几个配置或历史记录为空字符串时,一切正常。 从pgAdmin执行相同语句可以正常工作。
我猜问题在于PostgreSQL JDBC驱动程序,将空字符串视为bytea值是否有合理的原因?也许是Postgres 8和9之间的一些奇怪变化?
PreparedStatement.setXXX()
方法。您也可以尝试使用8.1 JDBC驱动程序。顺便说一句:您是否知道8.1已不再受支持? - user330315