{"id":16265911,"url":"https://github.com/fuxingloh/hibernate-postgres-jsonb","last_synced_at":"2025-07-26T22:15:59.528Z","repository":{"id":78901725,"uuid":"78433855","full_name":"fuxingloh/hibernate-postgres-jsonb","owner":"fuxingloh","description":"Using Hibernate with Postgres JSONB","archived":false,"fork":false,"pushed_at":"2017-01-09T15:07:17.000Z","size":71,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T00:49:50.914Z","etag":null,"topics":["hibernate","jsonb","postgres-jsonb"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fuxingloh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-09T14:00:43.000Z","updated_at":"2025-03-04T14:27:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"f9b73707-5c17-4a08-a747-91bb7af2cc26","html_url":"https://github.com/fuxingloh/hibernate-postgres-jsonb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuxingloh%2Fhibernate-postgres-jsonb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuxingloh%2Fhibernate-postgres-jsonb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuxingloh%2Fhibernate-postgres-jsonb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuxingloh%2Fhibernate-postgres-jsonb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fuxingloh","download_url":"https://codeload.github.com/fuxingloh/hibernate-postgres-jsonb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247862723,"owners_count":21008750,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hibernate","jsonb","postgres-jsonb"],"created_at":"2024-10-10T17:21:06.046Z","updated_at":"2025-04-08T14:38:28.565Z","avatar_url":"https://github.com/fuxingloh.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Hibernate Postgres JSONB\n\nA working implementation of JSONB with Hibernate and Jackson ObjectNode.\n\u003cbr /\u003e\nThe library address the problem of using Hibernate with Postgres JSONB \n\nUsing Postgres JSONB in HibernateJPA\n- Hibernate + JPA\n- Postgres + JSONB\n- Jackson + (ObjectNode or CustomPOJO)\n\nOnly 2 class file!\n- JsonPostgreSQLDialect\n- JsonUserType\n\n### Setup \u0026 Test\nSetup JSONB\n```xml\n\u003c!-- Set dialect to org.hibernate.dialect.JsonPostgreSQLDialect --\u003e\n\u003cproperty name=\"hibernate.dialect\" value=\"org.hibernate.dialect.JsonPostgreSQLDialect\"/\u003e\n```\n\nSetup Entity Class\n```java\n@TypeDef(name = \"jsonb\", typeClass = JsonUserType.class)\n@Entity\npublic class JsonEntity {\n    @Type(type = \"jsonb\")\n    private ObjectNode json;\n}\n```\n\nRun test with Postgres Docker container and gradlew test\n```bash\ndocker run -d -p 32978:5432 -e POSTGRES_USER=jsonb-user -e POSTGRES_PASSWORD=6w51SG476dfd --name jsonb-database postgres\ngradlew test\n```\n\n### Some Examples\n\n#### JsonEntity\n```java\n@TypeDef(name = \"jsonb\", typeClass = JsonUserType.class)\n@Entity\npublic class JsonEntity {\n    private String id;\n    private String name; // normal field\n    private ObjectNode json; // jsonb\n\n    @GeneratedValue(generator = \"uuid\")\n    @GenericGenerator(name = \"uuid\", strategy = \"org.hibernate.id.UUIDGenerator\")\n    @Column(columnDefinition = \"CHAR(36)\", nullable = false, updatable = false)\n    @Id\n    public String getId() {\n        return id;\n    }\n\n    protected void setId(String id) {\n        this.id = id;\n    }\n\n    @Column(nullable = true)\n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    @Type(type = \"jsonb\")\n    @Column(nullable = true)\n    public ObjectNode getJson() {\n        return json;\n    }\n\n    public void setJson(ObjectNode json) {\n        this.json = json;\n    }\n}\n```\n\n##### Usage example with JsonEntity\nLook at JsonEntityTest for more info\n```java\nclass TestExample{\n    static ObjectMapper mapper = new ObjectMapper();\n    EntityManager entityManager; // Provide your own EntityManager instance\n    \n    /**\n     * How to store jackson ObjectNode\n     */\n    @Test\n    void persistJson() throws Exception {\n        JsonEntity entity = new JsonEntity();\n        entity.setName(\"my name\");\n\n        // Create object node and populate\n        ObjectNode node = mapper.createObjectNode();\n        node.put(\"parser\", \"jackson\");\n        entity.setJson(node);\n\n        // Persist and get generated unique id\n        entityManager.persist(entity);\n        final String id = entity.getId();\n\n        // Query and get object node back\n        JsonEntity queryEntity = entityManager.find(JsonEntity.class, id);\n        ObjectNode queryNode = queryEntity.getJson();\n    }\n    \n    /**\n     * How to store custom pojo object?\n     */\n    @Test\n    void persistObject() throws Exception {\n        MyCustomObject object = new MyCustomObject();\n        \n        // Persist entity\n        JsonEntity entity = new JsonEntity();\n        entity.setJson(mapper.valueToTree(object));\n        entityManager.persist(entity);\n        \n        // Query entity\n        JsonEntity queryEntity = entityManager.find(JsonEntity.class, entity.getId());\n        MyCustomObject queryObject = mapper.treeToValue(queryEntity.getJson(), MyCustomObject.class);\n        assertEquals(queryObject, object);\n    }\n}\n```\n\nReferences:\nhttps://github.com/pires/hibernate-postgres-jsonb\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffuxingloh%2Fhibernate-postgres-jsonb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffuxingloh%2Fhibernate-postgres-jsonb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffuxingloh%2Fhibernate-postgres-jsonb/lists"}