项目作者: hakuna16

项目描述 :
JSONB crud example using spring data jpa
高级语言: Java
项目地址: git://github.com/hakuna16/jsonB-example.git
创建时间: 2021-05-02T08:34:59Z
项目社区:https://github.com/hakuna16/jsonB-example

开源协议:

下载


Project Detail

This project tries to cover all the Scenerios for the JSONB implementaion which developers will be facing in day to day life when using spring data JPA.

What is JSONB

The data types json and jsonb, as defined by the PostgreSQL documentation,are almost identical; the key difference is that json data is stored as an exact copy of the JSON input text, whereas jsonb stores data in a decomposed binary form; that is, not as an ASCII/UTF-8 string, but as binary code.

What are the benifits of JSONB

  • More efficiency,
  • Significantly faster to process,
  • Supports indexing (which can be a significant advantage, as we’ll see later),
  • Simpler schema designs (replacing entity-attribute-value (EAV) tables with jsonb columns, which can be queried, indexed and joined, allowing for performance improvements up until 1000X!)

What are the drawbacks of JSONB

  • Slightly slower input (due to added conversion overhead),
  • It may take more disk space than plain json due to a larger table footprint, though not always,
  • Certain queries (especially aggregate ones) may be slower due to the lack of statistics.

Scenerios

  • Working on simple json document using jsonb
  • Working on complex json document using jsonb

Please see StudentController.java as a starting for that.

We have used dependency from https://github.com/vladmihalcea/hibernate-types for modeling our pojo’s.

  1. <dependency>
  2. <groupId>com.vladmihalcea</groupId>
  3. <artifactId>hibernate-types-52</artifactId>
  4. <version>2.10.4</version>
  5. </dependency>

For jsonb type we have to define the columns type as jsonb as follows:

  1. @Type(type = "jsonb")

Project Setup:

  • clone this repo.
  • Update the database configutaions.
  • Create student table using below query:

    1. CREATE TABLE student (
    2. id varchar(255) NOT NULL,
    3. age varchar(255) NULL,
    4. "name" varchar(255) NULL,
    5. bio jsonb NULL,
    6. CONSTRAINT student_pkey PRIMARY KEY (id)
    7. );
  • Insert scripts used:

  1. INSERT INTO public.student
  2. (id, age, "name", bio)
  3. VALUES('1', '26', 'hakuna_matata', '{"professional": {"xyz": true}, "personal": {"xyx": "test"}}');
  • Run the project in console using mvn spring-boot:run