PostgreSQL array object mapping through Hibernate

User defined datatype in hibernate

Posted on August 16, 2014
In this post I am providing code for PostgreSQL array datatype mapping through hibernate. All the explanation is same as my previous post Custom User Type Class
package com.vivekpatidar.postgres.demo;

import java.sql.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.usertype.UserType;

public class IntegerArrayType implements UserType {

    public Object assemble(Serializable cached, Object owner)
            throws HibernateException {
        return this.deepCopy(cached);

    public Object deepCopy(Object value) throws HibernateException {
        return value;

    public Serializable disassemble(Object value) throws HibernateException {
        return (Integer[]) this.deepCopy(value);

    public boolean equals(Object x, Object y) throws HibernateException {

        if (x == null) {
            return y == null;
        return x.equals(y);

    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();

    public boolean isMutable() {
        return true;

    public Object nullSafeGet(ResultSet resultSet, String[] names,
            SessionImplementor session, Object owner)
            throws HibernateException, SQLException {
        Array array = resultSet.getArray(names[0]);
        Integer[] javaArray = (Integer[]) array.getArray();
        return javaArray;

    public void nullSafeSet(PreparedStatement statement, Object value,
            int index, SessionImplementor session) throws HibernateException,
            SQLException {
        Connection connection = statement.getConnection();
        Integer[] castObject = (Integer[]) value;
        Array array = connection.createArrayOf("integer", castObject);
        statement.setArray(index, array);

    public Object replace(Object original, Object target, Object owner)
            throws HibernateException {
        return original;

    public Class<Integer[]> returnedClass() {
        return Integer[].class;

    public int[] sqlTypes() {
        return new int[] { Types.ARRAY };

Custom Dialect
package com.vivekpatidar.postgres.demo;

import java.sql.Types;
import org.hibernate.dialect.PostgreSQL9Dialect;

public class ArrayPostgreSQLDialect extends PostgreSQL9Dialect {

    public ArrayPostgreSQLDialect() {

         * For other type array you can change integer[] to that array type

        this.registerColumnType(Types.ARRAY, "integer[]");

UserContact Hibernate Entity Class
package com.vivekpatidar.postgres.demo;

import java.util.Arrays;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

@TypeDefs({ @TypeDef(name = "IntegerArrayObject", typeClass = IntegerArrayType.class) })
public class UserContact {

    private Long id;

    @Type(type = "IntegerArrayObject")
    private Integer[] contact;

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public Integer[] getContact() {
        return contact;

    public void setContact(Integer[] contact) { = contact;

    public String toString() {
        return "UserContact [id=" + id + ", contact="
                + Arrays.toString(contact) + "]";

Hibernate Session Factory Implementation class
package com.vivekpatidar.postgres.demo;

import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class PostgresHibernateUtil {
    private static Configuration configuration = new Configuration();
    private static SessionFactory sessionFactory;
    private static Properties prop;

    private static SessionFactory buildSessionFactory() {
        try {

            prop = new Properties();

            String url = "";
            prop.put("hibernate.connection.url", "jdbc:postgresql://" + url);

            prop.put("hibernate.connection.username", "postgres");
            prop.put("hibernate.connection.password", "root");

            prop.put("hibernate.show_sql", "true");
            prop.put("", "update");
            prop.put("hibernate.connection.isolation", "2");



            return configuration.buildSessionFactory();
        } catch (Throwable ex) {
            System.out.println("Initital session factory build failed." + ex);
            throw new ExceptionInInitializerError(ex);

    public static SessionFactory getSessionFactory() {
        return buildSessionFactory();

    public static Session getSession() {
        return sessionFactory.getCurrentSession();