Java library easy querying jsonb PostgreSQL type of data.
This lib can be used either along with Hibernate CriteriaBuilder API (CriteriaBuilder), Hibernate Criterion API (Restrictions) or with native JDBC queries.
More on how to use PostgreSQL JSON data types along with this lib you can read on my blog.
In order to use pg-jsonb lib with CriteriaBuilder API it is required to use one from the provided Dialects that could be found at com.github.borsch.pgjson.hibernate.dialect
.
For example:
hibernate.dialect=com.github.borsch.pgjson.hibernate.dialect.PostgresJsonSQL94Dialect
//JPA/Hibernate stuff
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<User> cr = cb.createQuery(User.class);
Root<User> root = cr.from(User.class);
// Instantiate JSONRootImpl with RootImpl. This is required in order to build json properties
JSONRootImpl<User> jsonRoot = new JSONRootImpl<User>((RootImpl<User>) root, "json_column_name");
//query json data
cr.where(cb.equal(jsonRoot.get("jsondata.parent[0].name"), "Jane Doe"));
As you can see in example, json expressions are build using JSONRootImpl<X>
.
Methods get(String jsonPath, Class<Y> type)
and get(String jsonPath)
will build a json paths. If type is not specified, String is used by default.
API is using javascript notation to build paths, i.e:
jsondata.parent.nested.object.array[1]
Criteria criteria = ... // Get Criteria object
criteria.add(Restrictions.eq("name", "John Doe"); // Regular hibernate query
criteria.add(JSONBRestrictions.eq("json_column", "jsondata.parent[0].name", "Jane Doe"); // Json restriction
criteria.list(); // Obtain result
Statement stmt = conn.createStatement();
SimpleJsonCondition jsonCondition = new SimpleJsonCondition(new JsonProperty("json_column", "sondata.parent[0].name"), new ParametrizedValue("Jane Doe"), "=");
String sql = new StringBuilder()
.append("SELECT * FROM user WHERE ")
.append(jsonCondition.toSqlString())
.toString();
Add to your pom.xml
<dependency>
<groupId>com.github.borsch.pgjsonb</groupId>
<artifactId>pgjsonb</artifactId>
<version>${pgjsonb-version}</version>
</dependency>
In order to run tests, there are few prerequirements needed:
- run Docker Desktop/Docker engine (required for test-containers)
- Database
pgjsonb
- User test/test with full access to
pgjsonb
database - Execute seed.sql from test/resources
- Maven - Dependency Management
Feel free to enhance this libary and open PR. I'll review PRs as soon as possible.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details