How to convert SQL rows to jakarta.json.JsonObject?

Viewed 19

We are trying to perform basic SQL queries (SELECT statements) and convert them into a JsonObject, or a List<JsonObject>, or JsonArray.

    public Optional<JsonArray> getByGuid(UUID guid) throws SQLException {
        try (var conn = this.dataSource.getConnection();
             var stmt = conn.prepareStatement("SELECT * FROM table WHERE guid = ?;");
        ) {
            stmt.setObject(1, guid);
            var rs = stmt.executeQuery();
            if (!rs.next())
                return Optional.empty();
            JsonArray jsonArray = resultSetToJsonObjects(rs);
            return Optional.of(jsonArray);
        }
    }

The resulting jsonObject should be similar to a JPA entity converted with Jackson::ObjectMapper. We can't use JPA entities.

In summary, it would be the same as using regular entities but with JsonObject objects instead.

We don't want to bring the complete JOOQ library to the app. But it's possible to use Hibernate (no entities please, just JPQL).

0 Answers
Related