How to use PostgreSQL’s JSONB data type with Hibernate

Most databases offer lots of proprietary features in addition to the known SQL standard. One example for it is PostgreSQL’s JSONB data type which allows you to store JSON documents efficiently in a database column.

You could of course also store the JSON document in a text column (which is part of the SQL standard and supported by Hibernate), but then you would miss out on PostgreSQL-specific features like JSON validation and a list of interesting JSON Functions and Operators. But you are probably already aware of that if you are reading this post.

Unfortunately, Hibernate does not support PostgreSQL’s JSONB data type, and you have to implement an UserType for it. I will show you how to do that in this post.

Database table and entity

Let’s have a quick look at the database table and entity before we get into the details of the UserType.
As you can see in the following code snippet, the definition of the database table is very simple and consists of only 2 columns. The primary key column id and the column jsonproperty of type JSONB.

So what do you have to do, if you want to store the MyJson property in a JSONB database column?

Implement a Hibernate UserType

The first thing you have to do is to create a Hibernate UserType, which maps the MyJson object into a JSON document and defines the mapping to an SQL type. I call the UserType MyJsonType and show only the most important methods in the following code snippets. You can have a look at the entire class in the GitHub repository. There are a few important things you have to do if you want to implement your own UserType. First of all, you have to implement the methods sqlTypes and returnedClass which tell Hibernate the SQL type and the Java class it shall use for this mapping. In this case, I use the generic Type.JAVA_OBJECT as the SQL type and of course the MyJson class as the Java class.

Then you have to implement the methods nullSafeGet and nullSafeSet which Hibernate will call when you read the attribute from and when you write it to the database. The nullSafeGet method gets called to map the value from the database into the Java class. So we have to parse the JSON document into a MyJson class. I use the Jackson ObjectMapper here, but you can also use any other JSON parser. The nullSafeSet method implements the mapping of the MyJson class into the JSON document, and I again use the Jackson ObjectMapper for it.

Another important method you need to implement is the deepCopy method which has to create a deep copy of a MyJson object. One of the easiest ways to do that is to serialize and deserialize the MyJson object which requires the JVM to create a deep copy of the object.

Register the UserType

In the next step, you need to register your custom UserType. You can do this with a @TypeDef annotation in the package-info.java file. As you can see in the following code snippet, I set the name and the typeClass property of the @TypeDef annotation.

Hibernate will now use the UserTypeMyJsonType to persist the jsonproperty attribute in the database. But there is still one step left.

Hibernate dialect

Hibernate’s PostgreSQL dialect does not support the JSONB datatype, and you need to create your own dialect to register it. But don’t worry, there isn’t much to do because you can simply extend an existing dialect and call the registerColumnType method in the constructor. I’m using a PostgreSQL database in version 9.4 in this example and therefore extend the existing PostgreSQL94Dialect.

How to use an entity with a JSONB mapping

You can use the MyEntity entity and its MyJson attribute in the same way as any other entity and attribute. You can, for example, use the EntityManager.find method to get an entity from the database and then change the attribute values of the MyJson object.

Summary and cheat sheet

PostgreSQL offers different proprietary datatypes, like the JSONB type I used in this post, to store JSON documents in the database. These datatypes are not supported by Hibernate and you have to implement the mapping yourself. As you have seen during this post, this can be done by implementing the UserType interface, registering it with a @TypeDef annotation and creating a Hibernate dialect which registers the column type.

Hi Thorben, another fascinating article, thank you for sharing it. I have 2 questions: would this same method work with the Json support in newer versions of mysql? And do you think hibernate itself will add Json support in a future version? Cheers, martin

An AttributeConverter is much simpler. Unfortunately, you can only use it with Hibernate to convert any Java type to a different Java type that’s already supported by Hibernate. So, you can’t map the class to PostgreSQL’s JSONB type. You would need to map it to, e.g. a String.

I had the same problem and adding typedef in the entity class worked, but does that mean my application is not able to get it from package-info.java file.
PS : I am working on a spring boot application

It’s up to you how you implement the UserType. How do you want to handle the unknown structure of the document?
You could skip the JSON processing step (the part with the ObjectMapper) and just map it to a String.