how to store PostgreSQL jsonb using SpringBoot JPA?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
PostgreSQL jsonb is a good fit when part of your data is flexible but still needs indexing and query support. In a modern Spring Boot application using Hibernate 6, the cleanest mapping is usually a normal entity field annotated for JSON and backed by a PostgreSQL jsonb column.
Define the jsonb Column in PostgreSQL
Start with a table that uses jsonb explicitly:
Using jsonb instead of plain json gives you better indexing and richer operator support in PostgreSQL.
Map JSON with Hibernate 6
In current Spring Boot setups that use Hibernate 6, you can map JSON with @JdbcTypeCode:
JsonNode is a good choice when the structure is flexible. If the JSON shape is stable, a dedicated Java class can be even better.
Save and Load the Entity
A regular Spring Data JPA repository works as expected:
Example usage:
From the application side, it behaves like any other mapped field.
Querying jsonb
JPA itself does not provide first-class abstractions for every PostgreSQL JSON operator, so native queries are often the pragmatic choice:
That gives you access to PostgreSQL’s jsonb operators without forcing awkward application-side filtering.
Index the JSON Paths You Query
If you query inside JSON regularly, index for it. A broad GIN index can help:
For specific paths, expression indexes can be better:
Without indexing, jsonb can become convenient to write but expensive to search.
When to Avoid jsonb
jsonb is useful, but it is not a license to stop modeling data. If a field is required, heavily queried, and structurally stable, a normal relational column is often better. jsonb is strongest where:
- shape varies legitimately
- partial semi-structured attributes exist
- the document changes over time
Treat it as a complement to the relational model, not a full replacement for it.
Common Pitfalls
- Storing stable relational fields in
jsonbjust because it feels flexible. - Forgetting
columnDefinition = "jsonb"and ending up with the wrong database type. - Assuming JPQL will cover every PostgreSQL JSON operator cleanly.
- Skipping indexes on JSON paths that are frequently queried.
- Using an untyped JSON blob everywhere when part of the structure is actually stable enough for a real Java model.
Summary
- Use a real PostgreSQL
jsonbcolumn when semi-structured data belongs in the database. - In Hibernate 6,
@JdbcTypeCode(SqlTypes.JSON)is the modern mapping approach. - Map flexible payloads as
JsonNodeor a dedicated Java type, depending on how stable the schema is. - Use native PostgreSQL JSON operators when querying inside the document.
- Add the right indexes so
jsonbremains operationally useful, not just convenient to store.
Related reading
- How to stream data from Kafka to MongoDB by Kafka Connector
- How to subtract 30 days from the current datetime in mysql?
- How to switch databases in psql?
- How to switch Keyspace in Cassandra using CQL?
- How to synchronize data between two tables in different databases
- How to synchronize distributed system data across cassandra clusters
- how to take a keyspace as a dump in cassandra?
- How to take a merge replication back up?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.