更新java中的json列会引发类型异常

vwoqyblh  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(259)

我正在尝试更新一个json列并完全替换json。使用pbobject方法插入记录。但我一直得到下面的错误。

  1. 22-Dec-2020 09:03:15.087 WARNING [http-nio-9095-exec-1] util.server.exception.GenericEclipselinkExceptionHandler.handleException
  2. Internal Exception: org.postgresql.util.PSQLException: ERROR: column "json_col" is of type json but expression is of type character varying
  3. Hint: You will need to rewrite or cast the expression.
  4. Position: 44
  5. Error Code: 0
  6. Call: UPDATE my_table SET JSON_COL = ? WHERE (ID = ?)
  7. bind => [2 parameters bound]
  8. Query: UpdateObjectQuery(service.JsonRecord@7e67b84d)

更新如下所示:

  1. PGobject pGobject = new PGobject();
  2. pGobject.setType("json");
  3. pGobject.setValue(record.getJsonDoc());
  4. doInsideTransaction(
  5. -> {
  6. Query query = em.createNativeQuery(
  7. "update table set json_col=? where id=?"
  8. );
  9. query.setParameter(1, pGobject);
  10. query.setParameter(2, record.getId());
  11. query.executeUpdate();
  12. }
  13. );

哪个电话

  1. public void doInsideTransaction(UnaryAction<EntityManager> action) {
  2. EntityManager em = entityManagerProvider.get();
  3. runTx(em, () -> action.run(em));
  4. }
  5. public void runTx(EntityManager em, Action action) {
  6. EntityTransaction transaction = em.getTransaction();
  7. try {
  8. transaction.begin();
  9. action.run();
  10. transaction.commit();
  11. }
  12. catch (Exception error) {
  13. if (transaction.isActive()) {
  14. transaction.rollback();
  15. }
  16. throw error;
  17. }
  18. }

我也尝试过将json字符串转换为json和::json,但这也会返回相同的错误。
java 8,postgres 12.5。postgres驱动程序9.4。eclipselink 2.6.0版

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题