PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

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