有点晚了,但为了记录,这是我对@jasp提供的解决方案的版本。不同之处在于,此解决方案将一个Map对象作为JSON格式持久化到文本列中。它使用了Grails包含的Jackson库。
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
import java.io.IOException;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;
public class JSONStringType implements UserType {
private static final ObjectMapper _mapper = new ObjectMapper();
@Override
public int[] sqlTypes() {
return new int[] { Types.VARCHAR };
}
@SuppressWarnings("rawtypes")
@Override
public Class returnedClass() {
return String.class;
}
@Override
public boolean equals(Object x, Object y) throws HibernateException {
return (x != null) && x.equals(y);
}
@Override
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
try {
String val = rs.getString(names[0]);
return _mapper.readValue(val, Map.class);
} catch (IOException e) {
throw new HibernateException(e);
}
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
try {
String val;
if (value == null)
val = "{}";
else if (value instanceof String)
val = (String)value;
else
val = _mapper.writeValueAsString(value);
st.setObject(index, val, (value == null) ? Types.NULL : Types.VARCHAR);
} catch (JsonProcessingException e) {
throw new HibernateException(e);
}
}
@Override
public Object deepCopy(Object value) throws HibernateException {
if (value == null) return null;
try {
String val = _mapper.writeValueAsString(value);
return val;
} catch (JsonProcessingException e) {
throw new HibernateException(e);
}
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable)value;
}
@Override
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
return cached;
}
@Override
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return deepCopy(original);
}
}
使用方法:
import your.package.JSONStringType
class Book {
String name
String isbn
Map attributes = [:]
static constraints = {
}
static mapping = {
attributes type: JSONStringType, sqlType: 'nvarchar(4000)'
}
}
更改 sqlType
以匹配您的数据库列类型。对于 SQL Server,nvarchar(4000)
用于高效的 JSON 文档查询,或者 nvarchar(MAX)
用于大型 JSON 文档存储。