Search code examples
springpostgresqlhibernateentitymanagercriteriaquery

Handling Null values in postgres for text array type column in org.hibernate.PropertyAccessException


we got a strange scenario in our Hibernate based web application.

On the Database postgresql,there is a column field of text[] type and some of the values are Null. In my entity class I have mapped it to String[] and When I run a CriteriaBuilder select query I am getting exception saying

Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not set a field value by reflection setter

I am using @Type(type = "GenericArrayUserType") annotation on String[] in entity class.


Solution

  • Please use the below custom GenericStringArrayUserType mapping for your String[] attribute.

     @Type(type = "ai.test.GenericStringArrayUserType")
     private String[] fields;
    
    import org.hibernate.HibernateException;
    import org.hibernate.engine.spi.SessionImplementor;
    import org.hibernate.usertype.UserType;
    
        public class GenericStringArrayUserType<T extends Serializable> implements UserType {
    
            protected static final int[] SQL_TYPES = {Types.ARRAY};
            private Class<T> typeParameterClass;
    
            @Override
            public int[] sqlTypes() {
                return new int[]{Types.ARRAY};
            }
    
            @Override
            public Class<T> returnedClass() {
                return typeParameterClass;
            }
    
            @Override
            public boolean equals(Object x, Object y) throws HibernateException {
    
                if (x == null) {
                    return y == null;
                }
                return x.equals(y);
            }
    
            @Override
            public int hashCode(Object x) throws HibernateException {
                return x.hashCode();
            }
    
            @Override
            public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner)
                    throws HibernateException, SQLException {
    
        /*
                if (resultSet.wasNull()) {
                    return null;
                }
        */
                if (resultSet.getArray(names[0]) == null) {
                    return new String[0];
                }
    
                Array array = resultSet.getArray(names[0]);
                @SuppressWarnings("unchecked")
                T javaArray = (T) array.getArray();
                return javaArray;
            }
    
            @Override
            public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
                    throws HibernateException, SQLException {
                Connection connection = statement.getConnection();
                if (value == null) {
                    statement.setNull(index, SQL_TYPES[0]);
                } else {
                    @SuppressWarnings("unchecked")
                    T castObject = (T) value;
                    Array array = connection.createArrayOf("text", (Object[]) castObject);
                    statement.setArray(index, array);
                }
            }
    
            @Override
            public Object deepCopy(Object value) throws HibernateException {
                return value;
            }
    
            @Override
            public boolean isMutable() {
                return true;
            }
    
            @SuppressWarnings("unchecked")
            @Override
            public Serializable disassemble(Object value) throws HibernateException {
                return (T) this.deepCopy(value);
            }
    
            @Override
            public Object assemble(Serializable cached, Object owner) throws HibernateException {
                return this.deepCopy(cached);
            }
    
            @Override
            public Object replace(Object original, Object target, Object owner) throws HibernateException {
                return original;
            }
    
    
        }