[英]How I can persist a json object using hibernate?
這是我的情況:我有一個帶有屬性的類,該屬性可以是json類型或其他數據結構。
public class MiClase {
private int a;
private Object b;
//Get and Set method
}
我想使用休眠方式持久化此類,但是我也想使用以下代碼:
ObjectMapper m=new ObjectMapper();
m.readValue(json, new TypeReference<MiClase>() {
});
上面的代碼工作正常,但是當我嘗試將實體保存到數據庫中時,出現以下錯誤:映射具有錯誤的列數:MiClase.b類型:object然后嘗試更改然后嘗試將de object type更改為byte [ ],但在使用readValue方法時不能反序列化byte []的實例。 在這種情況下,您推薦我什么策略? 我正在使用postgresql
我對問題的評估感到失望,我認為它具有所有邏輯,盡管我的英語不是很好,但是該解決方案對我有幫助,允許我將任何數據持久化到數據庫中:解決方案是創建自定義類型:
public class JacksonUserType implements UserType {
private static final int[] SQL_TYPES = { Types.LONGVARCHAR };
@Override
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) {
return true;
} else if (x == null || y == null) {
return false;
} else {
return x.equals(y);
}
}
@Override
public int hashCode(Object x) throws HibernateException {
return null == x ? 0 : x.hashCode();
}
@Override
public boolean isMutable() {
return true;
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
if (value == null) {
st.setString(index, null);
} else {
st.setString(index, convertObjectToJson(value));
}
}
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
String content = rs.getString(names[0]);
if(content!=null){
return convertJsonToObject(content);
}
return null;
}
Object convertJsonToObject(String content){
try {
ObjectMapper mapper = new ObjectMapper();
JavaType type = createJavaType(mapper);
return mapper.readValue(content, type);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
String convertObjectToJson(Object object){
try {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper.writeValueAsString(object);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public Object deepCopy(Object value) throws HibernateException {
String json = convertObjectToJson(value);
return convertJsonToObject(json);
}
/**
* Optionnal
*/
@Override
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return deepCopy(original);
}
/**
* (optional operation)
*
* @param value
*
* @throws HibernateException
*/
@Override
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) deepCopy(value);
}
/**
* (optional operation)
*
* @param cached
* @param owner
*
* @return the instance cached
*
* @throws HibernateException
*/
@Override
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
return deepCopy(cached);
}
/**
* By default we are expecting to use a simple object / not a collection (Set, List)
*
* @param mapper : instance jackson object mapper
*
* @return A jackson JavaType to specify wich object represent the json string representation
*
*/
public JavaType createJavaType (ObjectMapper mapper){
return SimpleType.construct(returnedClass());
}
@Override
public int[] sqlTypes() {
return SQL_TYPES;
}
@Override
public Class returnedClass() {
return Object.class;
}
}
您還可以將抽象類的變體與抽象方法一起使用:returnClass()而不使用通用Object類型
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.