Giter Site home page Giter Site logo

pg-jsonb's Introduction

PG - jsonb

Java library easy querying jsonb PostgreSQL type of data.

Getting Started

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.

Hibernate CriteriaBuilder API example

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]

Hibernate Criterion API example

  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

JDBC example

  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();

Installing

Add to your pom.xml

<dependency>
    <groupId>com.github.borsch.pgjsonb</groupId>
    <artifactId>pgjsonb</artifactId>
    <version>${pgjsonb-version}</version>
</dependency>

Running the tests

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

Built With

  • Maven - Dependency Management

Contributing

Feel free to enhance this libary and open PR. I'll review PRs as soon as possible.

Authors

  • Vedad Zornic - Initial work - vzornic
  • Oleh Kurpiak - Migration to Hibernate 6 & Java 17 - borsch

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

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.