Skip to content

Instantly share code, notes, and snippets.

@mugren
Created February 28, 2020 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mugren/a10f9a5a092c646e39c60027227f1a4b to your computer and use it in GitHub Desktop.
Save mugren/a10f9a5a092c646e39c60027227f1a4b to your computer and use it in GitHub Desktop.
LongArrayType - Hibernate User Type (bigint[])
import org.hibernate.HibernateException
import org.hibernate.engine.spi.SessionImplementor
import org.hibernate.usertype.UserType
import java.sql.*
class LongArrayType implements UserType {
@Override
int[] sqlTypes() {
return [Types.ARRAY] as int[]
}
@Override
Class returnedClass() {
return long[].class
}
@Override
boolean equals(Object x, Object y) throws HibernateException {
return x == y
}
@Override
int hashCode(Object x) throws HibernateException {
x == null ? 0 : x.hashCode()
}
@Override
Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
Array array = rs.getArray(names[0])
array.getArray() as long[]
}
@Override
void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
Connection connection = st.getConnection()
Array array = connection.createArrayOf("bigint", value as long[])
st.setArray(index, array)
}
@Override
Object deepCopy(Object value) throws HibernateException {
if(value == null) return null
(value as long[]).clone()
}
@Override
boolean isMutable() {
return false
}
@Override
Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value
}
@Override
Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached
}
@Override
Object replace(Object original, Object target, Object owner) throws HibernateException {
return original
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment