Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wenerme/postjava
Java work with PostgreSQL
https://github.com/wenerme/postjava
hibernate jsonb postgresql querydsl
Last synced: 3 months ago
JSON representation
Java work with PostgreSQL
- Host: GitHub
- URL: https://github.com/wenerme/postjava
- Owner: wenerme
- License: mit
- Created: 2018-06-13T03:50:44.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-06-13T06:50:33.000Z (over 6 years ago)
- Last Synced: 2024-10-01T10:22:27.125Z (4 months ago)
- Topics: hibernate, jsonb, postgresql, querydsl
- Language: Java
- Size: 14.6 KB
- Stars: 8
- Watchers: 4
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PostJava
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
```xml
com.github.wenerme.postjava
postjava
1.0.1```
```yaml
# Use the predefined dialect
spring.jpa.properties.hibernate.dialect: com.github.wenerme.postjava.hibernate.dialect.PostgreSQLJsonDialect
```Or use your customized dialect
```java
public class PostgreSQLCustomDialect extends PostgreSQLDialect {public PostgreSQLCustomDialect() {
super();
// Add json/jsonb support
PostgreSQLJsonDialect.register(this);
}
}
```### Define entity
* [vladmihalcea/hibernate-types](https://github.com/vladmihalcea/hibernate-types) provide json type support for hibernate```xml
com.vladmihalcea
hibernate-types-52
2.2.1```
```java
@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 labels;
}
```### Work with QueryDSL
```java
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
}
}
```