java – 使用hibernate将Json数据存储到Mysql 5.7数据库或从中检索Json数据
作者:互联网
我将开始一个项目,我需要生成动态谷歌表单.这个项目的要求是我需要使用mysql 5.7,以便我可以使用json数据类型来存储/检索json数据.我很好.我知道,有可能使用HQL.但我无法弄清楚如何使用对象关系映射实现Hibernate?
有任何想法吗 ?
提前致谢 !
解决方法:
最近我解决了类似的任务.我想现在为时已晚,但也许有人觉得这很有用.
简短回答:您应该创建必须实现org.hibernate.usertype.UserType接口的类(如“com.test.MyJsonType”),其中nullSafeGet方法应将JSON反序列化为java对象(使用Jackson),nullSafeSet将POJO序列化为JSON和一些其他辅助方法.
然后你应该扩展MySQLDialect并注册新的列类型“json”.
最后,您可以通过@Type(type =“com.test.MyJsonType”)注释实体字段,该字段应映射到MySQL json列.
如果您不希望使用包名称写入类型,也可以阅读@TypeDef here.
例如:
public class MyJsonType implements UserType {
@Override
public int[] sqlTypes() {
return new int[] { Types.VARCHAR };
}
@Override
public Class<Characteristics> returnedClass() {
return Characteristics.class;
}
@Override
public Object nullSafeGet(final ResultSet rs, final String[] names, final SessionImplementor session, final Object owner)
throws HibernateException, SQLException {
final String cellContent = rs.getString(names[0]);
if (cellContent == null) {
return null;
}
try {
final ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.readValue(cellContent.getBytes("UTF-8"), returnedClass());
} catch (final Exception ex) {
throw new RuntimeException("Failed to convert String to Invoice: " + ex.getMessage(), ex);
}
}
@Override
public void nullSafeSet(final PreparedStatement ps, final Object value, final int idx, final SessionImplementor session)
throws HibernateException, SQLException {
if (value == null) {
ps.setNull(idx, Types.VARCHAR);
return;
}
try {
final ObjectMapper mapper = new ObjectMapper();
final StringWriter w = new StringWriter();
mapper.writeValue(w, value);
w.flush();
ps.setObject(idx, w.toString(), Types.VARCHAR);
} catch (final Exception ex) {
throw new RuntimeException("Failed to convert Invoice to String: " + ex.getMessage(), ex);
}
}
@Override
public Object deepCopy(final Object value) throws HibernateException {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(value);
oos.flush();
oos.close();
bos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(bos.toByteArray());
return new ObjectInputStream(bais).readObject();
} catch (ClassNotFoundException | IOException ex) {
throw new HibernateException(ex);
}
}
@Override
public boolean isMutable() {
return true;
}
@Override
public Serializable disassemble(final Object value) throws HibernateException {
return (Serializable) this.deepCopy(value);
}
@Override
public Object assemble(final Serializable cached, final Object owner) throws HibernateException {
return this.deepCopy(cached);
}
@Override
public Object replace(final Object original, final Object target, final Object owner) throws HibernateException {
return this.deepCopy(original);
}
@Override
public boolean equals(Object x, Object y) throws HibernateException {
return Objects.equals(x, y);
}
@Override
public int hashCode(Object x) throws HibernateException {
return Objects.hashCode(x);
}
}
POJO课程:
public class Characteristics implements Serializable {
private String field;
public String getField() {
return field;
}
public void setField(String field) {
this.field= field;
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
final Characteristics other = (Characteristics) obj;
return Objects.equals(this.field, other.field);
}
@Override
public int hashCode() {
return Objects.hash(this.field);
}
}
注册新列类型:
public class JsonMySQLDialect extends MySQLDialect {
public JsonMySQLDialect() {
this.registerColumnType(Types.VARCHAR, "json");
}
}
使用:
@Entity
@Table(name = "Table")
public class TableClass {
...
@Column
@Type(type = "com.test.MyJsonType")
protected Characteristics characteristics;
...
}
标签:mysql-5-7,java,json,hibernate 来源: https://codeday.me/bug/20191005/1855464.html