在 org.hibernate.PropertyAccessException 中处理 postgres 中文本数组类型列的空值
Handling Null values in postgres for text array type column in org.hibernate.PropertyAccessException
我们在基于 Hibernate 的 Web 应用程序中遇到了一个奇怪的场景。
在数据库postgresql上,有一个text[]类型的列字段,部分值为Null。在我的实体 class 中,我已将其映射到 String[] 并且当我 运行 一个 CriteriaBuilder select 查询时,我收到异常提示
请求处理失败;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: 无法通过反射设置字段值 setter
我在实体 class 中的 String[] 上使用 @Type(type = "GenericArrayUserType") 注释。
请为您的 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;
}
}
我们在基于 Hibernate 的 Web 应用程序中遇到了一个奇怪的场景。
在数据库postgresql上,有一个text[]类型的列字段,部分值为Null。在我的实体 class 中,我已将其映射到 String[] 并且当我 运行 一个 CriteriaBuilder select 查询时,我收到异常提示
请求处理失败;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: 无法通过反射设置字段值 setter
我在实体 class 中的 String[] 上使用 @Type(type = "GenericArrayUserType") 注释。
请为您的 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;
}
}