PageRenderTime 54ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/common/src/main/java/com/sishuok/es/common/repository/hibernate/type/JsonMapUserType.java

https://bitbucket.org/beginnerjyh/es
Java | 152 lines | 75 code | 20 blank | 57 comment | 11 complexity | 4b03d712142452e08f72eccc4a44629c MD5 | raw file
  1. package com.sishuok.es.common.repository.hibernate.type;
  2. import com.alibaba.fastjson.JSON;
  3. import org.hibernate.HibernateException;
  4. import org.hibernate.engine.spi.SessionImplementor;
  5. import org.hibernate.usertype.UserType;
  6. import java.io.Serializable;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Types;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. /**
  14. * Json字符串---->Map
  15. * Map----->Json字符串
  16. * 参考http://jinnianshilongnian.iteye.com/blog/1497791
  17. * User: Zhang Kaitao
  18. * Date: 11-7-16
  19. * Time: 下午5:45
  20. */
  21. public class JsonMapUserType implements UserType, Serializable {
  22. // private static ObjectMapper objectMapper = new ObjectMapper();
  23. static {
  24. // objectMapper.enableDefaultTyping();
  25. }
  26. @Override
  27. public int[] sqlTypes() {
  28. return new int[]{Types.VARCHAR};
  29. }
  30. @Override
  31. public Class returnedClass() {
  32. return JsonMap.class;
  33. }
  34. @Override
  35. public boolean equals(Object o, Object o1) throws HibernateException {
  36. if (o == o1) {
  37. return true;
  38. }
  39. if (o == null || o == null) {
  40. return false;
  41. }
  42. return o.equals(o1);
  43. }
  44. @Override
  45. public int hashCode(Object o) throws HibernateException {
  46. return o.hashCode();
  47. }
  48. /**
  49. * 从JDBC ResultSet读取数据,将其转换为自定义类型后返回
  50. * (此方法要求对克能出现null值进行处理)
  51. * names中包含了当前自定义类型的映射字段名称
  52. *
  53. * @param names
  54. * @param owner
  55. * @return
  56. * @throws HibernateException
  57. * @throws SQLException
  58. */
  59. @Override
  60. public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
  61. String json = rs.getString(names[0]);
  62. // try {
  63. // Map<Object, Object> map = objectMapper.readValue(json, HashMap.class);
  64. // return new JsonMap(map);
  65. // } catch (IOException e) {
  66. // throw new HibernateException(e);
  67. // }
  68. Map<Object, Object> map = JSON.parseObject(json, HashMap.class);
  69. return new JsonMap(map);
  70. }
  71. /**
  72. * 本方法将在Hibernate进行数据保存时被调用
  73. * 我们可以通过PreparedStateme将自定义数据写入到对应的数据库表字段
  74. */
  75. @Override
  76. public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
  77. if (value == null) {
  78. st.setNull(index, Types.VARCHAR);
  79. } else {
  80. // try {
  81. // st.setString(index, objectMapper.writeValueAsString((((JsonMap) value).getMap())));
  82. // } catch (JsonProcessingException e) {
  83. // throw new HibernateException(e);
  84. // }
  85. st.setString(index, JSON.toJSONString((((JsonMap) value).getMap())));
  86. }
  87. }
  88. /**
  89. * 提供自定义类型的完全复制方法
  90. * 本方法将用构造返回对象
  91. * 当nullSafeGet方法调用之后,我们获得了自定义数据对象,在向用户返回自定义数据之前,
  92. * deepCopy方法将被调用,它将根据自定义数据对象构造一个完全拷贝,并将此拷贝返回给用户
  93. * 此时我们就得到了自定义数据对象的两个版本,第一个是从数据库读出的原始版本,其二是我们通过
  94. * deepCopy方法构造的复制版本,原始的版本将有Hibernate维护,复制版由用户使用。原始版本用作
  95. * 稍后的脏数据检查依据;Hibernate将在脏数据检查过程中将两个版本的数据进行对比(通过调用
  96. * equals方法),如果数据发生了变化(equals方法返回false),则执行对应的持久化操作
  97. *
  98. * @param o
  99. * @return
  100. * @throws HibernateException
  101. */
  102. @Override
  103. public Object deepCopy(Object o) throws HibernateException {
  104. if (o == null) return null;
  105. JsonMap map = new JsonMap();
  106. map.setMap(((JsonMap) o).getMap());
  107. return map;
  108. }
  109. /**
  110. * 本类型实例是否可变
  111. *
  112. * @return
  113. */
  114. @Override
  115. public boolean isMutable() {
  116. return true;
  117. }
  118. /* 序列化 */
  119. @Override
  120. public Serializable disassemble(Object value) throws HibernateException {
  121. return ((Serializable) value);
  122. }
  123. /* 反序列化 */
  124. @Override
  125. public Object assemble(Serializable cached, Object owner) throws HibernateException {
  126. return cached;
  127. }
  128. @Override
  129. public Object replace(Object original, Object target, Object owner) throws HibernateException {
  130. return original;
  131. }
  132. }