Skip to content

Instantly share code, notes, and snippets.

@mugren
Created February 28, 2020 17:26
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/ad5254c4f7043507c412be2d057fe08b to your computer and use it in GitHub Desktop.
Save mugren/ad5254c4f7043507c412be2d057fe08b to your computer and use it in GitHub Desktop.
JsonListType Hibernate Type using text column
import com.fasterxml.jackson.databind.JavaType
import com.fasterxml.jackson.databind.ObjectMapper
import org.hibernate.HibernateException
import org.hibernate.engine.spi.SessionImplementor
import org.hibernate.usertype.DynamicParameterizedType
import org.hibernate.usertype.UserType
import java.lang.reflect.Field
import java.lang.reflect.ParameterizedType
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Types
class JsonListType implements UserType, DynamicParameterizedType {
private static final int[] SQL_TYPES = [Types.LONGVARCHAR]
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
private JavaType valueType = null
private Class<?> classType = null
@Override
int[] sqlTypes() {
SQL_TYPES
}
@Override
Class returnedClass() {
return classType
}
@Override
boolean equals(Object x, Object y) throws HibernateException {
return x == y
}
@Override
int hashCode(Object x) throws HibernateException {
return Objects.hashCode(x)
}
@Override
Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
String value = rs.getString(names[0])
Object result = null
if (valueType == null) {
throw new HibernateException("Value type not set.")
}
if (!value) {
try {
result = OBJECT_MAPPER.readValue(value, valueType)
} catch (IOException e) {
throw new HibernateException("Exception de-serializing value " + value, e)
}
}
return result
}
@Override
void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
StringWriter sw = new StringWriter()
if (value == null) {
st.setNull(index, Types.VARCHAR)
} else {
try {
OBJECT_MAPPER.writeValue(sw, value)
st.setString(index, sw.toString())
} catch (IOException e) {
throw new HibernateException("Exception serializing value " + value, e)
}
}
}
@Override
Object deepCopy(Object value) throws HibernateException {
if (value == null) {
return null
} else if (valueType.isCollectionLikeType()) {
try {
Collection newValueCollection = value.getClass().newInstance() as Collection
newValueCollection.addAll(value as Collection)
return newValueCollection
} catch (InstantiationException e) {
throw new HibernateException("Failed to deep copy the collection-like value object.", e)
} catch (IllegalAccessException e) {
throw new HibernateException("Failed to deep copy the collection-like value object.", e)
}
}
return null
}
@Override
boolean isMutable() {
return true
}
@Override
Serializable disassemble(Object value) throws HibernateException {
return (Serializable) deepCopy(value)
}
@Override
Object assemble(Serializable cached, Object owner) throws HibernateException {
return deepCopy(cached)
}
@Override
Object replace(Object original, Object target, Object owner) throws HibernateException {
return deepCopy(original)
}
@Override
void setParameterValues(Properties parameters) {
try {
// Get entity class
Class<?> entityClass = Class.forName(parameters.getProperty(DynamicParameterizedType.ENTITY))
Field property = null
// Find the field
while(property == null && entityClass != null){
try {
property = entityClass.getDeclaredField(parameters.getProperty(DynamicParameterizedType.PROPERTY))
} catch (NoSuchFieldException e) {
entityClass = entityClass.getSuperclass()
}
}
if(property != null){
ParameterizedType listType = (ParameterizedType) property.getGenericType()
Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0]
valueType = OBJECT_MAPPER.getTypeFactory().constructCollectionType(ArrayList.class, listClass)
classType = List.class
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment