项目作者: fuxingloh

项目描述 :
Using Hibernate with Postgres JSONB
高级语言: Java
项目地址: git://github.com/fuxingloh/hibernate-postgres-jsonb.git
创建时间: 2017-01-09T14:00:43Z
项目社区:https://github.com/fuxingloh/hibernate-postgres-jsonb

开源协议:Apache License 2.0

下载


Hibernate Postgres JSONB

A working implementation of JSONB with Hibernate and Jackson ObjectNode.


The library address the problem of using Hibernate with Postgres JSONB

Using Postgres JSONB in HibernateJPA

  • Hibernate + JPA
  • Postgres + JSONB
  • Jackson + (ObjectNode or CustomPOJO)

Only 2 class file!

  • JsonPostgreSQLDialect
  • JsonUserType

Setup & Test

Setup JSONB

  1. <!-- Set dialect to org.hibernate.dialect.JsonPostgreSQLDialect -->
  2. <property name="hibernate.dialect" value="org.hibernate.dialect.JsonPostgreSQLDialect"></property>

Setup Entity Class

  1. @TypeDef(name = "jsonb", typeClass = JsonUserType.class)
  2. @Entity
  3. public class JsonEntity {
  4. @Type(type = "jsonb")
  5. private ObjectNode json;
  6. }

Run test with Postgres Docker container and gradlew test

  1. docker run -d -p 32978:5432 -e POSTGRES_USER=jsonb-user -e POSTGRES_PASSWORD=6w51SG476dfd --name jsonb-database postgres
  2. gradlew test

Some Examples

JsonEntity

  1. @TypeDef(name = "jsonb", typeClass = JsonUserType.class)
  2. @Entity
  3. public class JsonEntity {
  4. private String id;
  5. private String name; // normal field
  6. private ObjectNode json; // jsonb
  7. @GeneratedValue(generator = "uuid")
  8. @GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
  9. @Column(columnDefinition = "CHAR(36)", nullable = false, updatable = false)
  10. @Id
  11. public String getId() {
  12. return id;
  13. }
  14. protected void setId(String id) {
  15. this.id = id;
  16. }
  17. @Column(nullable = true)
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. @Type(type = "jsonb")
  25. @Column(nullable = true)
  26. public ObjectNode getJson() {
  27. return json;
  28. }
  29. public void setJson(ObjectNode json) {
  30. this.json = json;
  31. }
  32. }
Usage example with JsonEntity

Look at JsonEntityTest for more info

  1. class TestExample{
  2. static ObjectMapper mapper = new ObjectMapper();
  3. EntityManager entityManager; // Provide your own EntityManager instance
  4. /**
  5. * How to store jackson ObjectNode
  6. */
  7. @Test
  8. void persistJson() throws Exception {
  9. JsonEntity entity = new JsonEntity();
  10. entity.setName("my name");
  11. // Create object node and populate
  12. ObjectNode node = mapper.createObjectNode();
  13. node.put("parser", "jackson");
  14. entity.setJson(node);
  15. // Persist and get generated unique id
  16. entityManager.persist(entity);
  17. final String id = entity.getId();
  18. // Query and get object node back
  19. JsonEntity queryEntity = entityManager.find(JsonEntity.class, id);
  20. ObjectNode queryNode = queryEntity.getJson();
  21. }
  22. /**
  23. * How to store custom pojo object?
  24. */
  25. @Test
  26. void persistObject() throws Exception {
  27. MyCustomObject object = new MyCustomObject();
  28. // Persist entity
  29. JsonEntity entity = new JsonEntity();
  30. entity.setJson(mapper.valueToTree(object));
  31. entityManager.persist(entity);
  32. // Query entity
  33. JsonEntity queryEntity = entityManager.find(JsonEntity.class, entity.getId());
  34. MyCustomObject queryObject = mapper.treeToValue(queryEntity.getJson(), MyCustomObject.class);
  35. assertEquals(queryObject, object);
  36. }
  37. }

References:
https://github.com/pires/hibernate-postgres-jsonb