However, while you can create your own custom Hibernate Types, for mapping PostgreSQL arrays, you don’t need to implement your own Hibernate Type. All you need to do is use the Hibernate Types open-source project.
Considering we have the following table in our PostgreSQL database schema:
create table event (
id int8 not null,
primary key (id)
We want to map this table using JPA and Hibernate. However, neither JPA nor Hibernate support SQL arrays by default, and we want to map these arrays to a String and int Java arrays, respectively. So, we are going to use the Hibernate Types project to achieve this goal.
The first thing you need to do is to set up the following Maven dependency in your project pom.xml configuration file:
If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.
While JPA defines the AttributeConverter interface, the standard converters are way too limited when it comes to mapping arrays or JSON types. That’s why the Hibernate Types project uses the Hibernate API to provide additional column type mappings.