JSONB crud example using spring data jpa
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.
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.
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.
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.10.4</version>
</dependency>
For jsonb type we have to define the columns type as jsonb as follows:
@Type(type = "jsonb")
Create student table using below query:
CREATE TABLE student (
id varchar(255) NOT NULL,
age varchar(255) NULL,
"name" varchar(255) NULL,
bio jsonb NULL,
CONSTRAINT student_pkey PRIMARY KEY (id)
);
Insert scripts used:
INSERT INTO public.student
(id, age, "name", bio)
VALUES('1', '26', 'hakuna_matata', '{"professional": {"xyz": true}, "personal": {"xyx": "test"}}');
mvn spring-boot:run