如何使用PostgreSQL hstore / json和JdbcTemplate

时间:2014-01-27 14:30:08

标签: java spring postgresql jdbctemplate hstore

有没有办法将PostgreSQL json / hstore与JdbcTemplate一起使用? esp查询支持。

例如:

hstore:

INSERT INTO hstore_test (data) VALUES ('"key1"=>"value1", "key2"=>"value2", "key3"=>"value3"')

SELECT data -> 'key4' FROM hstore_test
SELECT item_id, (each(data)).* FROM hstore_test WHERE item_id = 2

表示Json

insert into jtest (data) values ('{"k1": 1, "k2": "two"}');
select * from jtest where data ->> 'k2' = 'two';

2 个答案:

答案 0 :(得分:21)

虽然答案很晚(对于插入部分),但我希望它对其他人有用:

获取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\"}");

使用以下方法之一将它们插入PostgreSQL:

1)

jdbcTemplate.update(conn -> {
     PreparedStatement ps = conn.prepareStatement( "INSERT INTO table (hstore_col, jsonb_col) VALUES (?, ?)" );
     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) 在POJO中设置hstoreMap / jsonbObj(类型为Map的hstoreCol和jsonbObjCol的类型为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 );

获得价值:

(Map<String, String>) rs.getObject( "hstore_col" ));
((PGobject) rs.getObject("jsonb_col")).getValue();

答案 1 :(得分:1)

JdbcTemplate更容易,您可以使用hibernate-types开源项目来保留HStore属性。

首先,您需要Maven依赖项:

<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-52</artifactId>
    <version>${hibernate-types.version}</version>
</dependency>

然后,假设您具有以下Book实体:

@Entity(name = "Book")
@Table(name = "book")
@TypeDef(name = "hstore", typeClass = PostgreSQLHStoreType.class)
public static class Book {

    @Id
    @GeneratedValue
    private Long id;

    @NaturalId
    @Column(length = 15)
    private String isbn;

    @Type(type = "hstore")
    @Column(columnDefinition = "hstore")
    private Map<String, String> properties = new HashMap<>();

    //Getters and setters omitted for brevity
}

请注意,我们用properties注释了@Type实体属性,并指定了先前通过hstore定义的@TypeDef类型来使用{{1} }自定义休眠类型。

现在,在存储以下PostgreSQLHStoreType实体时:

Book

Hibernate执行以下SQL INSERT语句:

Book book = new Book();

book.setIsbn("978-9730228236");
book.getProperties().put("title", "High-Performance Java Persistence");
book.getProperties().put("author", "Vlad Mihalcea");
book.getProperties().put("publisher", "Amazon");
book.getProperties().put("price", "$44.95");

entityManager.persist(book);

而且,当我们获取INSERT INTO book (isbn, properties, id) VALUES ( '978-9730228236', '"author"=>"Vlad Mihalcea", "price"=>"$44.95", "publisher"=>"Amazon", "title"=>"High-Performance Java Persistence"', 1 ) 实体时,我们可以看到所有属性均已正确获取:

Book

有关更多详细信息,请查看this article