How to map JSON collections using JPA and Hibernate

(Last Updated On: February 2, 2018)


The open-source hibernate-types project allows you to map Java objects or Jackson JsonNode as JPA or Hibernate entity properties, and, thanks to our awesome contributors, we have added support for storing type-safe JSON collections.

In this article, you are going to see how to achieve this goal.

Maven dependency

First of all, you need to set up the following Maven dependency in your project pom.xml configuration file:


If you’re using older versions of Hibernate, check out the hibernate-types GitHub repository for more info about the matching dependency for your current Hibernate version.

Domain Model

Let’s assume we have the following Location Java object type.

public class Location implements Serializable {

    private String country;

    private String city;

    //Getters and setters omitted for brevity

    public String toString() {
        return "Location{" +
                "country='" + country + ''' +
                ", city='" + city + ''' +

And, one Event entity:

@Entity(name = "Event")
@Table(name = "event")
public class Event extends BaseEntity {

    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    private Location location;

    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    private List<Location> alternativeLocations = new ArrayList<Location>();

    //Getters and setters omitted for brevity

The BaseEntity defines some basic properties (e.g. @Id, @Version) and several custom Hibernate types, among which, we are interested in the JsonBinaryType one.

    @TypeDef(name = "string-array", typeClass = StringArrayType.class),
    @TypeDef(name = "int-array", typeClass = IntArrayType.class),
    @TypeDef(name = "json", typeClass = JsonStringType.class),
    @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class),
    @TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class),
    @TypeDef(name = "json-node", typeClass = JsonNodeStringType.class),
public class BaseEntity {

    private Long id;

    private Integer version;

    //Getters and setters omitted for brevity

For more details about using @MappedSuperclass, check out this article.

To store both the Location object or the List<Location> in a jsonb PostgreSQL column, we just need to annotate the location property with @Type(type = "jsonb").

That’s it!

Testing time

When saving the following Event entity:

Location cluj = new Location();

Location newYork = new Location();

Location london = new Location();

Event event = new Event();
    Arrays.asList(newYork, london)


Hibernate will generate the following SQL INSERT statement:


Also, when retrieving back the Event entity, both the location and thealternativeLocations` properties are properly fetched:

Event event = entityManager.find(Event.class, eventId);


assertEquals(2, event.getAlternativeLocations().size());


Cool, right?

If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.


The hibernate-types project supports more than JSON types. You can map PostgreSQL ARRAY types or PostgreSQL-specific Enums, nullable Character, or even provide your own immutable Hibernate custom Types.

Subscribe to our Newsletter

* indicates required
10 000 readers have found this blog worth following!

If you subscribe to my newsletter, you'll get:
  • A free sample of my Video Course about running Integration tests at warp-speed using Docker and tmpfs
  • 3 chapters from my book, High-Performance Java Persistence, 
  • a 10% discount coupon for my book. 
Get the most out of your persistence layer!


14 thoughts on “How to map JSON collections using JPA and Hibernate

  1. @Type(type = “jsonb”)
    @Column(columnDefinition = “jsonb”)
    private Map<String, String> stuff;

    Hey Vlad. Quick question, would the implementation of HashMap/Map work with jsonb as shown in the code above?

    1. I have never tested it with a Map, but you can give it a try and see if it works. If it does not, send me a Pull Request with the enhancement. Thanks.

  2. Really really cool Vlad, congrats!!!

    I’m trying to do the same but I’m getting :
    IllegalArgumentException: Not a managed type

    public class Sample extends BaseEntity {

    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Type(type = “jsonb”)
    @Column(columnDefinition = “jsonb”)
    private List exams = new ArrayList();


    Same as you did. There is any Git repo where I could see more samples? 🙂
    I shall really appreciate it 🙂 Thanks.

  3. I’m trying to use your library in wildfly 11.0.0.Final that uses hibernate 5.1.10.Final., but i’m facing some problems: when i bootstrap the application the result is
    java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.XProperty.

    Can you give me some tips about my problem?

    1. I have never used Wildfly. However, you just need to make sure you have the hibernate-commons-annotations artifact in your application class-path.

  4. Hi vlad, thanks for your great article.
    I encountered a problem here, I use hibernate and liquibase:

    2018-04-15 15:25:56.017 ERROR 21184 — [ main] o.h.metamodel.internal.MetadataContext : HHH015007: Illegal argument on static metamodel field injection :; expected type : org.hibernate.metamodel.internal.SingularAttributeImpl; encountered type : javax.persistence.metamodel.ListAttribute

    my domain class abilities field is as below:
    @Type(type = “json”)
    @Column(columnDefinition = “json”)
    private List abilities = new ArrayList();

    Could you please help? very very appreicated.

      1. I tried 5.2.17-SNAPSHOT, I cannot see the original exception, however a new exception: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [abilities] in table [ability], but the table has abilities indeed, don’t konw why.

  5. Hi Vlad,
    Here I am trying with Mysql 5.7 . The same example I am using.

    If I am trying with string type “json” then inserting in to the database is going fine. But while fetching I am getting the following issue.

    java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to my.spring.boot.application.model.Location

    If I am trying with binary type “jsonb”, then getting the data from database is going fine and conversion is happening fine. But while inserting in to database I am getting following exception. com.fasterxml.jackson.databind.node.ArrayNode

    please help me here Vlad.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.