3
How to perform object recovery and saving using type columns array
using the Hibernate
? In my case I want to save String
. How I define in model
the object? I found it on the net some examples but they didn’t work.
My class userType
extend of other, so 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 bank name
ufsinss character(2)[]
Version of the JDBC
postgresql-9.3-1101.jdbc41
Has submitted an error in the part of nullSafeSet
.
EDIT 1:
After Googlear a little bit, I found the following. The guy on this line was wrong, it was moved to, Array array = connection.createArrayOf("bpchar", stringArray);
. I found out by looking at the SO.com
. By searching for the array on nullSafeGet
using that line array.getBaseTypeName();
. My problem right now is nullSet
, when the vector comes null.
EDIT 2:
Finally what was missing was to put this code for when the array
voidable. st.setNull(index, Types.ARRAY);
Below I will post the reply with the description of the idea. Please we need and be helped give an up vote on my reply.
I don’t understand anything.. Could you explain better? Post the code of your attempts here instead of posting links.
– Math
@Math the code is the same as the website. What I want to do is this, on
postgres
you can define a field of typevetor
in my case this defined asvetor
ofString
. Natively theHibernate
does not have support, wanted to know how to do it. I saw that it is possible usingUserType
but mine of error.– Macario1983
It doesn’t seem like a good option, in JPA (which abstracts Hibernate for example) if you define the relationship you can use a multivalloyed attribute (array, List, etc...) and it will generate several rows in the other table, so you are more free.
– prmottajr
@prmottajr but would like to use in this way.
– Macario1983