Postgres JDBC 读取 Hibernate 编写的 TEXT 专栏

Postgres JDBC read TEXT column written by Hibernate

我必须使用 Flyway 迁移 Postgres 数据库。其中一个 table 包含一个 TEXT 列,其中包含由 Hibernate 写入的 XML 数据。由于在 Flyway 中手头只有 java.sql.Connection,因此您只能使用普通的 jdbc.

在这里您可以看到一个试图读取数据的片段:

    String query = "SELECT calc_proc_id, calc_key, parameters AS parameters FROM calculation WHERE parameters_json IS NULL";
    int counter = 0;
    try (Statement stmt = connection.createStatement())
    {
        try (ResultSet rs = stmt.executeQuery(query))
        {
            ResultSetMetaData resultSetMetaData = rs.getMetaData();
            int columnType = resultSetMetaData.getColumnType(3);
            String columnTypeName = resultSetMetaData.getColumnTypeName(3);
            String columnName = resultSetMetaData.getColumnName(3);
            logger.info("%s has %d (%s)", columnName, columnType, columnTypeName);
            while (rs.next())
            {
                UUID calcProcId = rs.getObject("calc_proc_id", UUID.class);
                String calcKey = rs.getString("calc_key");
                long oid = rs.getLong("parameters");
                try (LargeObject large = lom.open(oid, LargeObjectManager.READ))
    

到达此打开时,出现以下异常:

org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:337) ~[postgresql-42.2.9.jar:42.2.9]
    at org.postgresql.jdbc.PgConnection.executeTransactionCommand(PgConnection.java:829) ~[postgresql-42.2.9.jar:42.2.9]
    at org.postgresql.jdbc.PgConnection.rollback(PgConnection.java:872) ~[postgresql-42.2.9.jar:42.2.9]
    at org.flywaydb.core.internal.jdbc.TransactionTemplate.execute(TransactionTemplate.java:90) ~[flyway-core-6.0.8.jar:?]
    at org.flywaydb.core.internal.command.DbMigrate.applyMigrations(DbMigrate.java:279) ~[flyway-core-6.0.8.jar:?]
    at org.flywaydb.core.internal.command.DbMigrate.migrateGroup(DbMigrate.java:244) ~[flyway-core-6.0.8.jar:?]
    at org.flywaydb.core.internal.command.DbMigrate.access0(DbMigrate.java:54) ~[flyway-core-6.0.8.jar:?]
    at org.flywaydb.core.internal.command.DbMigrate.call(DbMigrate.java:162) ~[flyway-core-6.0.8.jar:?]
    at org.flywaydb.core.internal.command.DbMigrate.call(DbMigrate.java:159) ~[flyway-core-6.0.8.jar:?]
    at org.flywaydb.core.internal.database.postgresql.PostgreSQLAdvisoryLockTemplate.execute(PostgreSQLAdvisoryLockTemplate.java:71) ~[flyway-core-6.0.8.jar:?]
...
Caused by: java.io.IOException: Unexpected packet type: 0
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2390) ~[postgresql-42.2.9.jar:42.2.9]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:310) ~[postgresql-42.2.9.jar:42.2.9]
    ... 147 more

有没有人以前见过这个并且知道如何解决?

我以前没见过这个错误,但据我所知,你应该能够通过简单地调用 getClob("parameters")

来读取 TEXT 列