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

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

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

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

更新如下所示:

PGobject pGobject = new PGobject();
    pGobject.setType("json");
    pGobject.setValue(record.getJsonDoc());

    doInsideTransaction(
             -> {
                Query query = em.createNativeQuery(
                        "update table set json_col=? where id=?"
                );
                query.setParameter(1, pGobject);
                query.setParameter(2, record.getId());
                query.executeUpdate();
            }
    );

哪个电话

public void doInsideTransaction(UnaryAction<EntityManager> action) {
        EntityManager em = entityManagerProvider.get();
        runTx(em, () -> action.run(em));
    }

    public void runTx(EntityManager em, Action action) {
        EntityTransaction transaction = em.getTransaction();
        try {
            transaction.begin();
            action.run();
            transaction.commit();
        }
        catch (Exception error) {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            throw error;
        }
    }

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

暂无答案!

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

相关问题