How to perform object retrieval and saving using columns of type array
using Hibernate
? In my case I want to save String
. How do I set% object_to%? I found the net some examples but they did not work.
My class model
extends from another, so it does not implement all methods.
public class ArrayStringType extends TypeHibernate {
public static final String TYPE = "arrayStringType";
public Class<String[]> returnedClass() {
return String[].class;
}
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor sessionImpl, Object obj) throws HibernateException, SQLException {
Array array = rs.getArray(names[0]);
return NullUtil.isNull(array) ? null : (String[]) array.getArray();
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor sessionImpl) throws HibernateException, SQLException {
Connection connection = st.getConnection();
String[] stringArray = (String[]) value;
if (NullUtil.isNull(stringArray)) {
st.setNull(index, CharArrayType.INSTANCE.sqlType());
} else {
Array array = connection.createArrayOf("char", stringArray);
st.setArray(index, array);
}
}
}
Column type and its name in the bank
ufsinss character(2)[]
JDBC Version
postgresql-9.3-1101.jdbc41
You have displayed an error in the userType
part.
EDIT 1:
After a little Google, I found the following. The type on this line was wrong, it was raised to nullSafeSet
. I discovered looking at Array array = connection.createArrayOf("bpchar", stringArray);
. By searching for the array in SO.com
using this line nullSafeGet
. My problem now is array.getBaseTypeName();
, when the vector comes null.
EDIT 2:
Finally what was missing was to place this code for when nullSet
is null. array
Below I will post the answer with the description of the idea. Please feel free to give me an up vote on my answer.