2

We are trying to define a json column at hibernate as follows:

The entity class:

@TypeDef( name="CustomType", CustomJSONType.class)
...
@Type( name = "CustomType")
@Column
private JSONObject myColumn;
...

The CustomJSONType class implements UserType.

When application runs, the logs show me the following error trace:

2018-02-06 08:46:44.067 ERROR [company-operations-module,,,] 11804 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: create table operation (id int4 not null, creation timestamp not null, type varchar(50) not null, request_data json not null, response_data json, status varchar(50) not null, user_id varchar(30), primary key (id)) 
2018-02-06 08:46:44.067 ERROR [operations-module,,,] 11804 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : Unknown data type: "JSON" Unknown data type: "JSON"; SQL statement: create table operation (id int4 not null, creation timestamp not null, type varchar(50) not null, request_data json...) [50004-190] 
2018-02-06 08:46:44.067  INFO [operations-module,,,] 11804 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete

Any idea? Thanks in advance.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
silvia Dominguez
  • 445
  • 1
  • 4
  • 16
  • which database are you using? Is your DB supports JSON datatype? if not then in that case you may have to implement converter. – Bilbo Baggins Feb 06 '18 at 08:39
  • The database is Posgress, but the error occurs when we execute the test. In this case, we are using an in-memory hibernate database. – silvia Dominguez Feb 06 '18 at 08:41
  • I found this, this uses H2 database. https://stackoverflow.com/questions/39620317/how-can-solve-json-column-in-h2 – Bilbo Baggins Feb 06 '18 at 09:19

2 Answers2

7

It is also possible to configure H2 to take JSON as text using the 'create domain' option. For example, in the connection URL:

jdbc:h2:~/test;AUTO_SERVER=TRUE;INIT=create domain if not exists json as text
Vlad
  • 103
  • 1
  • 6
4

H2 database do not support JSON data type. So, your tests are failing.

One work around which I suggest you is to store the JSON column as TEXT and process data as required.

krisnik
  • 1,406
  • 11
  • 18
  • 1
    Another workaround which I can suggest you is to use Postgres Docker image https://docs.docker.com/samples/library/postgres/ for running your tests. This will add some overhead, but worth a try. – krisnik Feb 06 '18 at 09:02
  • 1
    Great! Please do accept the answer if it has solved your issue. – krisnik Feb 06 '18 at 13:12