Skip to content

Commit eb03338

Browse files
committed
Merge pull request #58 from kaleidos/feature/jsonb
Add support for jsonb
2 parents e5764dc + 51950f8 commit eb03338

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jdk:
44
- oraclejdk8
55

66
addons:
7-
postgresql: "9.3"
7+
postgresql: "9.4"
88

99
before_script:
1010
- psql -d template1 -c 'create extension hstore;' -U postgres

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Currently the plugin supports array, hstore and json fields as well as some quer
3333
* [JSON](#json)
3434
* [Criterias](#criterias)
3535
* [Has field value](#has-field-value)
36+
* [JSONB](#jsonb)
3637
* [Authors](#authors)
3738
* [Release Notes](#release-notes)
3839

@@ -510,6 +511,30 @@ The previous criteria will return all the rows that have a `name` attribute in t
510511

511512

512513

514+
#### JSONB
515+
516+
Since version postgresql-extensions 4.4.0 it is possible to using [Postgresql Jsonb](http://www.postgresql.org/docs/9.4/static/datatype-json.html)
517+
instead of just json. You need to use at least Postgresql 9.4.
518+
519+
To define a jsonb field you only have to define a `Map` field and use the `JsonbMapType` hibernate user type.
520+
521+
```groovy
522+
import net.kaleidos.hibernate.usertype.JsonbMapType
523+
524+
class TestMapJsonb {
525+
Map data
526+
527+
static constraints = {
528+
}
529+
static mapping = {
530+
data type: JsonbMapType
531+
}
532+
}
533+
```
534+
535+
The same criterias implemented for Json are valid for Jsonb.
536+
537+
513538
Authors
514539
-------
515540

@@ -524,6 +549,7 @@ Collaborations are appreciated :-)
524549
Release Notes
525550
-------------
526551

552+
* 4.4.0 - 15/Mar/2015 - Hibernate 4.x. Add support for Jsonb.
527553
* 3.3.0 - 18/Aug/2014 - Hibernate 3.x. Fix [#49](https://github.com/kaleidos/grails-postgresql-extensions/issues/49). Configure sequence per table or a global sequence for all tables.
528554
* 4.3.0 - 17/Aug/2014 - Hibernate 4.x. Fix [#49](https://github.com/kaleidos/grails-postgresql-extensions/issues/49). Configure sequence per table or a global sequence for all tables.
529555
* 3.2.0 - 02/Aug/2014 - Hiberate 3.x. pgJsonHasFieldValue criteria.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.json
2+
3+
import net.kaleidos.hibernate.usertype.JsonbMapType
4+
5+
class TestMapJsonb {
6+
7+
Map data
8+
9+
static constraints = {
10+
}
11+
static mapping = {
12+
data type: JsonbMapType
13+
}
14+
}

src/java/net/kaleidos/hibernate/PostgresqlExtensionsDialect.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.kaleidos.hibernate.usertype.ArrayType;
55
import net.kaleidos.hibernate.usertype.HstoreType;
66
import net.kaleidos.hibernate.usertype.JsonMapType;
7+
import net.kaleidos.hibernate.usertype.JsonbMapType;
78
import org.hibernate.dialect.Dialect;
89
import org.hibernate.dialect.PostgreSQL81Dialect;
910
import org.hibernate.id.PersistentIdentifierGenerator;
@@ -29,6 +30,7 @@ public PostgresqlExtensionsDialect() {
2930
registerColumnType(ArrayType.FLOAT_ARRAY, "float[]");
3031
registerColumnType(HstoreType.SQLTYPE, "hstore");
3132
registerColumnType(JsonMapType.SQLTYPE, "json");
33+
registerColumnType(JsonbMapType.SQLTYPE, "jsonb");
3234
}
3335

3436
/**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.kaleidos.hibernate.usertype;
2+
3+
public class JsonbMapType extends JsonMapType {
4+
5+
public static int SQLTYPE = 90022;
6+
7+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.kaleidos.hibernate.json
2+
import spock.lang.Specification
3+
import spock.lang.Unroll
4+
import test.json.TestMapJsonb
5+
6+
class PostgresqlJsonbMapDomainIntegrationSpec extends Specification {
7+
8+
@Unroll
9+
void 'save a domain class with a map #map to jsonb'() {
10+
setup:
11+
def testMapJsonb = new TestMapJsonb(data: map)
12+
13+
when:
14+
testMapJsonb.save(flush: true)
15+
16+
then:
17+
testMapJsonb.hasErrors() == false
18+
testMapJsonb.data == map
19+
20+
where:
21+
map << [null, [:], [name: 'Ivan', age: 34]]
22+
}
23+
24+
void 'save and read a domain class with jsonb'() {
25+
setup:
26+
def value = [name: 'Ivan', age: 34, hasChilds: true, childs: [[name: 'Judith', age: 7], [name: 'Adriana', age: 4]]]
27+
def testMapJsonb = new TestMapJsonb(data: value)
28+
29+
when:
30+
testMapJsonb.save(flush: true)
31+
32+
then:
33+
testMapJsonb.hasErrors() == false
34+
35+
and:
36+
def obj = testMapJsonb.get(testMapJsonb.id)
37+
obj.data.keySet().collect { it.toString() }.equals(['name', 'age', 'hasChilds', 'childs'])
38+
obj.data.childs.size() == 2
39+
}
40+
}

0 commit comments

Comments
 (0)