处理org.hibernate.PropertyAccessException中的文本数组类型列的postgres中的Null值

问题描述 投票:0回答:1

我们在基于Hibernate的Web应用程序中遇到了一个奇怪的情况。

在数据库postgresql上,有text []类型的列字段,其中一些值为Null。在我的实体类中,我已将其映射到String [],当我运行CriteriaBuilder选择查询时,出现了异常,说

请求处理失败;嵌套异常是javax.persistence.PersistenceException:org.hibernate.PropertyAccessException:无法通过反射设置器设置字段值

我正在实体类中的String []上使用@Type(type =“ GenericArrayUserType”)批注。

spring postgresql hibernate entitymanager criteriaquery
1个回答
1
投票

请为您的String []属性使用下面的自定义GenericStringArrayUserType映射。

 @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;
        }


    }
© www.soinside.com 2019 - 2024. All rights reserved.