Hibernate JSON UserType: simple handling of JSON objects with Jackson

Fabrizio Fortino bio photo By Fabrizio Fortino Comment

Hibernate provides a wide range of mappings to convert SQL data to Java objects and the other way around. In some circumstances, developers may need to define custom types to handle the mapping of a column to a specific type. In the last years, along with the exploding growth of JavaScript, JSON has become the most popular data­-interchange format with lots of supporting libraries that make it easy to read and write models in JSON. Jackson is a widely used high performance Java JSON data processing library. Here I will show you how to create a custom Hibernate UserType that uses Jackson to map generic POJOs to a database column in a transparent fashion.

Add Jackson as dependency to your project

Include the Jackson UserType implementation

Here is the code in charge of handling the marshall/unmarshall of JSON to Java and vice versa. It implements ParameterizedType to allow parameterization of the mapping file. This implementation supports LONGVARCHAR, CLOB and BLOB.

Define the hbm file to instruct Hibernate how to map the defined class to the database table

Below is an example of mapping file for the Person java class. As you can see it contains the status property of type EnumType (provided by Hibernate) and our JSONUserType config property. Parameter classType is the mapped POJO and type is the CLOB column type constant (see class java.sql.Types for more details on constant field values).

comments powered by Disqus