https://github.com/fuxingloh/hibernate-postgres-jsonb
Using Hibernate with Postgres JSONB
https://github.com/fuxingloh/hibernate-postgres-jsonb
hibernate jsonb postgres-jsonb
Last synced: 11 months ago
JSON representation
Using Hibernate with Postgres JSONB
- Host: GitHub
- URL: https://github.com/fuxingloh/hibernate-postgres-jsonb
- Owner: fuxingloh
- License: apache-2.0
- Created: 2017-01-09T14:00:43.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-09T15:07:17.000Z (over 9 years ago)
- Last Synced: 2025-04-02T00:49:50.914Z (about 1 year ago)
- Topics: hibernate, jsonb, postgres-jsonb
- Language: Java
- Homepage:
- Size: 69.3 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## 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
```xml
```
Setup Entity Class
```java
@TypeDef(name = "jsonb", typeClass = JsonUserType.class)
@Entity
public class JsonEntity {
@Type(type = "jsonb")
private ObjectNode json;
}
```
Run test with Postgres Docker container and gradlew test
```bash
docker run -d -p 32978:5432 -e POSTGRES_USER=jsonb-user -e POSTGRES_PASSWORD=6w51SG476dfd --name jsonb-database postgres
gradlew test
```
### Some Examples
#### JsonEntity
```java
@TypeDef(name = "jsonb", typeClass = JsonUserType.class)
@Entity
public class JsonEntity {
private String id;
private String name; // normal field
private ObjectNode json; // jsonb
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
@Column(columnDefinition = "CHAR(36)", nullable = false, updatable = false)
@Id
public String getId() {
return id;
}
protected void setId(String id) {
this.id = id;
}
@Column(nullable = true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Type(type = "jsonb")
@Column(nullable = true)
public ObjectNode getJson() {
return json;
}
public void setJson(ObjectNode json) {
this.json = json;
}
}
```
##### Usage example with JsonEntity
Look at JsonEntityTest for more info
```java
class TestExample{
static ObjectMapper mapper = new ObjectMapper();
EntityManager entityManager; // Provide your own EntityManager instance
/**
* How to store jackson ObjectNode
*/
@Test
void persistJson() throws Exception {
JsonEntity entity = new JsonEntity();
entity.setName("my name");
// Create object node and populate
ObjectNode node = mapper.createObjectNode();
node.put("parser", "jackson");
entity.setJson(node);
// Persist and get generated unique id
entityManager.persist(entity);
final String id = entity.getId();
// Query and get object node back
JsonEntity queryEntity = entityManager.find(JsonEntity.class, id);
ObjectNode queryNode = queryEntity.getJson();
}
/**
* How to store custom pojo object?
*/
@Test
void persistObject() throws Exception {
MyCustomObject object = new MyCustomObject();
// Persist entity
JsonEntity entity = new JsonEntity();
entity.setJson(mapper.valueToTree(object));
entityManager.persist(entity);
// Query entity
JsonEntity queryEntity = entityManager.find(JsonEntity.class, entity.getId());
MyCustomObject queryObject = mapper.treeToValue(queryEntity.getJson(), MyCustomObject.class);
assertEquals(queryObject, object);
}
}
```
References:
https://github.com/pires/hibernate-postgres-jsonb