Obwohl recht spät für eine Antwort (für das Einsatzteil), hoffe ich, könnte es nützlich jemand anderes sein :
Nehmen Sie die Schlüssel/Wert-Paare in einer HashMap:
Map<String, String> hstoreMap = new HashMap<>();
hstoreMap.put("key1", "value1");
hstoreMap.put("key2", "value2");
PGobject jsonbObj = new PGobject();
jsonbObj.setType("json");
jsonbObj.setValue("{\"key\" : \"value\"}");
Verwendung einer der folgenden Art und Weise, sie zu PostgreSQL einzufügen:
1)
jdbcTemplate.update(conn -> {
PreparedStatement ps = conn.prepareStatement("INSERT INTO table (hstore_col, jsonb_col)");
ps.setObject(1, hstoreMap);
ps.setObject(2, jsonbObj);
});
2)
jdbcTemplate.update("INSERT INTO table (hstore_col, jsonb_col) VALUES(?,?)",
new Object[]{ hstoreMap, jsonbObj }, new int[]{Types.OTHER, Types.OTHER});
3) Set hstoreMap/jsonbObj im POJO (hstoreCol vom Typ Map und jsonbObjCol ist vom Typ PGObject)
BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(POJO);
sqlParameterSource.registerSqlType("hstore_col", Types.OTHER);
sqlParameterSource.registerSqlType("jsonb_col", Types.OTHER);
namedJdbcTemplate.update("INSERT INTO table (hstore_col, jsonb_col) VALUES (:hstoreCol, :jsonbObjCol)", sqlParameterSource);
Und um den Wert zu erhalten:
(Map<String, String>) rs.getObject("hstore_col"));
((PGobject) rs.getObject("jsonb_col")).getValue();
überraschend, dass es für mich immer noch nicht funktioniert hat. Ein Upgrade auf den neuesten postgres-Versionstreiber hat das behoben. – linqu