【问题标题】:Handling Null values in postgres for text array type column in org.hibernate.PropertyAccessException处理 org.hibernate.PropertyAccessException 中文本数组类型列的 postgres 中的 Null 值
【发布时间】:2019-12-19 15:31:30
【问题描述】:

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

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

请求处理失败;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not set a field value by reflection setter

我在实体类中的 String[] 上使用 @Type(type = "GenericArrayUserType") 注释。

【问题讨论】:

    标签: spring postgresql hibernate entitymanager criteriaquery


    【解决方案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;
            }
    
    
        }
    

    【讨论】:

    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 2016-02-04
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多