com.github.alexliesenfeld:querydsl-jpa-postgres-json

Querydsl extension for using PostgreSQL JSON types with JPA

License

License

MIT
Categories

Categories

Querydsl Data Databases JSON
GroupId

GroupId

com.github.alexliesenfeld
ArtifactId

ArtifactId

querydsl-jpa-postgres-json
Last Version

Last Version

0.0.7
Release Date

Release Date

Type

Type

jar
Description

Description

com.github.alexliesenfeld:querydsl-jpa-postgres-json
Querydsl extension for using PostgreSQL JSON types with JPA
Project URL

Project URL

https://github.com/alexliesenfeld/querydsl-jpa-postgres-json
Source Code Management

Source Code Management

http://github.com/alexliesenfeld/querydsl-jpa-postgres-json

Download querydsl-jpa-postgres-json

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.alexliesenfeld/querydsl-jpa-postgres-json/ -->
<dependency>
    <groupId>com.github.alexliesenfeld</groupId>
    <artifactId>querydsl-jpa-postgres-json</artifactId>
    <version>0.0.7</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.alexliesenfeld/querydsl-jpa-postgres-json/
implementation 'com.github.alexliesenfeld:querydsl-jpa-postgres-json:0.0.7'
// https://jarcasting.com/artifacts/com.github.alexliesenfeld/querydsl-jpa-postgres-json/
implementation ("com.github.alexliesenfeld:querydsl-jpa-postgres-json:0.0.7")
'com.github.alexliesenfeld:querydsl-jpa-postgres-json:jar:0.0.7'
<dependency org="com.github.alexliesenfeld" name="querydsl-jpa-postgres-json" rev="0.0.7">
  <artifact name="querydsl-jpa-postgres-json" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.alexliesenfeld', module='querydsl-jpa-postgres-json', version='0.0.7')
)
libraryDependencies += "com.github.alexliesenfeld" % "querydsl-jpa-postgres-json" % "0.0.7"
[com.github.alexliesenfeld/querydsl-jpa-postgres-json "0.0.7"]

Dependencies

compile (2)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind Optional jar 2.10.0
org.projectlombok : lombok Optional jar 1.18.8

provided (2)

Group / Artifact Type Version
com.querydsl : querydsl-jpa jar 4.2.1
org.hibernate : hibernate-entitymanager jar 5.2.10.Final

Project Modules

There are no modules declared in this project.

querydsl-jpa-postgres-json

This repository contains a Querydsl extension for working with JSON types when using JPA, Hibernate and PostgreSQL. It is based on https://github.com/wenerme/postjava but extends it to support more data types.

Java work with PostgreSQL

  • Single class to rule both json and jsonb
  • Hibernate json/jsonb dialect registry
  • json/jsonb operator for QueryDSL
  • json/jsonb function integration for QueryDSL

Get started

Install dialect

<dependency>
    <groupId>com.github.alexliesenfeld</groupId>
    <artifactId>querydsl-jpa-postgres-json</artifactId>
    <version>0.0.7</version>
</dependency>
# Use the predefined dialect
spring.jpa.properties.hibernate.dialect: com.github.alexliesenfeld.querydsl.jpa.hibernate.PostgreSQLJsonDialect

Or use your customized dialect

public class PostgreSQLCustomDialect extends PostgreSQLDialect {

  public PostgreSQLCustomDialect() {
    super();
    // Add json/jsonb support
    PostgreSQLJsonDialect.register(this);
  }
}

Define entity

<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-52</artifactId>
    <version>2.2.1</version>
</dependency>
@TypeDefs({
  @TypeDef(name = "json", typeClass = JsonStringType.class),
  @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class),
  @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class),
  @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class),
})
@Entity
@Table(name = "users")
@Setter
@Getter
class UserEntity {
  Integer id;
  @Type(type="json-node")
  JsonNode attributes;
  @Type(type="jsonb")
  Map<String,String> labels;
}

Work with QueryDSL

class PlayJson{
  public static void main(String[] args) {
    // Will auto detect json/jsonb
    JsonPath attrs = JsonPath.of(QUserEntity.userEntity.attributes);
    attrs.get("name").asText().like("wener%"); // String expression
    attrs.get("age").asInt().gt(18); // Integer expression
    attrs.get("score").asFloat().gt(1.5); // Float expression
    attrs.get("resources").contain(1).isTrue(); // Is array contain element 
    attrs.get("resources").contain("A").isTrue().not(); // Is array not contain element 
    attrs.get("resources").contain("A").isFalse(); // Is array not contain element
    attrs.get("resources").length().gt(0); // Is array length > 0
  }
}

License

querydsl-jpa-postgres-json is free software: you can redistribute it and/or modify it under the terms of the MIT Public License.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MIT Public License for more details.

Versions

Version
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1