Hibernate HSQLDB ARRAY Type

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!


As previously explained, although Hibernate does not support database ARRAY column types by default, you can easily implement a custom Hibernate ArrayType. While the previous solution worked on PostgreSQL, in this article, you are going to see that it is fairly easy to adapt it to HSQLDB.

Domain Model

Assuming we have the following Event entity in our system:

We need a way to persist the String[] and Integer[] entity attributes since Hibernate, by default, does not support database-specific ARRAY types.

For that, we are going to use two custom Hibernate types:

@Entity(name = "Event")
@Table(name = "event")
        name = "string-array", 
        typeClass = VarCharStringArrayType.class
        name = "int-array", 
        typeClass = IntArrayType.class
public class Event {

    private Long id;

    @Type(type = "string-array")
        name = "sensor_names", 
        columnDefinition = "VARCHAR(100) ARRAY"
    private String[] sensorNames;

    @Type(type = "int-array")
        name = "sensor_values", 
        columnDefinition = "INT ARRAY"
    private Integer[] sensorValues;

    //Getters and setters omitted for brevity

The code for StringArrayType and IntArrayType has been described before in this article so we are not going to repeat it here.

The only difference is that we are using the VarCharStringArrayType now since HSQLDB does not support the TEXT database column type:

public class VarCharStringArrayType
        extends StringArrayType {

    public static final VarCharStringArrayType INSTANCE = 
        new VarCharStringArrayType();

    public VarCharStringArrayType() {

Also the VarCharStringArrayTypeDescriptor is rather trivial too:

public class VarCharStringArrayTypeDescriptor
        extends StringArrayTypeDescriptor {

    public static final VarCharStringArrayTypeDescriptor INSTANCE = 
            new VarCharStringArrayTypeDescriptor();

    public String getSqlArrayType() {
        return "varchar";

And, that’s it!

You don't have to create all these types manually. You can simply get them via Maven Central using the following dependency:


For more info, check out the hibernate-types open-source project.

Testing time

When persisting two Event entities:

Event nullEvent = new Event();


Event event = new Event();
    new String[]{"Temperature", "Pressure"}
    new Integer[]{12, 756}


Hibernate is going to execute the following SQL INSERT statements:



When fetching the Event entity:

Event event = entityManager.find(Event.class, 1L);

assertArrayEquals(new String[]{"Temperature", "Pressure"}, event.getSensorNames());
assertArrayEquals(new Integer[]{12, 756}, event.getSensorValues());

Hibernate can properly map the underlying ARRAY column type to the String[] and Integer[] Java arrays.

You can also use JPQL to filter the results based on a given Java array:

Event event = entityManager
    "select e " +
    "from Event e " +
    "where e.sensorNames = :sensorNames", Event.class)
    new String[]{"Temperature", "Pressure"}

I'm running an online workshopk on the 14th of May about The Best Way to Fetch Data with Java Persistence and Hibernate.

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


As demonstrated in this article, mapping ARRAY columns types to Java String[] or Integer arrays is fairly easy when using Hibernate.

Transactions and Concurrency Control eBook

7 Comments on “Hibernate HSQLDB ARRAY Type

  1. Hello Mr vladmihalcea,
    i want to Insert into my Postgresql a Float-Array in but i dont find a typeClass =FloatArray. How i can manage it?

    • There’s a pending Pull Request that aims to add this feature. I’ll review it for the 2.9.4 release when O have some time. In the meanwhile, you can add the type yourself in your application to avoid waiting for the next release.

  2. javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: Could not set field value [[Ljava.lang.Object;@56781d96] value by reflection : [class com.guinevisitaturistica.guinevisitabackend.dto.Hotel.code] setter of com.guinevisitaturistica.guinevisitabackend.dto.Hotel.code

    Hi sir how are you I am having some uses with Listing my records in database, the field CODE wich is array of string throws an error javax.persistence.PersistenceException when I try to test the List method.

    • I’m fine, thanks. Check out my consulting page if you are interested in hiring me to help you with your issue.

      • thank you sir for answer me as fast as you can, actually I am working on my school project so I really need help for this, please I need to find the solution .

      • All the best then. Study my blog and you will learn more.

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.