Giter Club home page Giter Club logo

postgres-json-jpa's Introduction

spring-data-jpa usage of Postgres JSONB fields

This simple project can be used as PoC in storing generically data in JSONB fields. In a scenario where profession specific information would be stored in joined tables, with the help of JSONB fields in Postgres we can store all the information in one table and avoid the table joins needed to retrieve all the required information about a person.

What this PoC brings new is that specific entities :

  • Student
  • Professor

can be handled in a type-safe manner :

  • Student class has a StudentInfo field
  • Professor class has a ProfessorInfo field

On the other hand, if some batch processes need to deal with all the persons, this can be done with the help of CommonPerson class (and associated CommonPersonRepository).

The table containing the persons (generated here by Hibernate) looks like this :

CREATE TABLE person
(
  dtype character varying(31) NOT NULL,
  id bigint NOT NULL,
  email character varying(255),
  info jsonb,
  CONSTRAINT person_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);

Incovenients

The main inconvenient in using JSON fields seen in this project is that the json fields can not be queried (at least when using hibernate as JPA provider) via JPQL queries. The support of Postgres for JSON fields being considered specific (most of the other database engines don't deal with JSON/JSONB fields) lead to not having introduced direct support for it in JPA.

This PoC there should give an idea on how to store generic data in a single table by using JSON fields, but it seems clear that, in order to query the data, native (Postgres specific) SQL should be used.

Similar projects

psql-cache-evict-POC project (via eclipselink JPA provider) offers the possibility to execute queries related to JSON fields :

        String jpql = "SELECT c FROM Course c where SQL('course_mapped ->> ''?'' = ''Second one''',c.name) ";

On the other hand, hibernate JPA provider doesn't support such constructs.

Environment requirements

  • Java 8
  • Spring Boot 1.3.2.RELEASE
  • Hibernate 5.0.2
  • PostgreSQL 9.4
  • Maven 3

postgres-json-jpa's People

Contributors

mariusneo avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.