Skip to content

Instantly share code, notes, and snippets.

@debop
Created September 29, 2013 04:09
Show Gist options
  • Save debop/6749264 to your computer and use it in GitHub Desktop.
Save debop/6749264 to your computer and use it in GitHub Desktop.
package kr.hconnect.data.hibernate.usertype
import org.hibernate.usertype.UserType
import java.io.Serializable
import java.sql.{Types, ResultSet, PreparedStatement}
import org.hibernate.engine.spi.SessionImplementor
import kr.hconnect.core.json.JsonTextObject
import java.util.Objects
import org.hibernate.`type`.StandardBasicTypes
/**
* kr.hconnect.data.hibernate.usertype.JsonUserType
* @author 배성혁 [email protected]
* @since 13. 9. 29. 오후 12:24
*/
class JsonUserType extends UserType {
val SQL_TYPES = Array(Types.VARCHAR, Types.VARCHAR)
def sqlTypes(): Array[Int] = SQL_TYPES
def returnedClass(): Class[_] = classOf[JsonTextObject]
def equals(x: Any, y: Any) = Objects.equals(x, y)
def hashCode(x: Any) = Objects.hashCode(x)
def nullSafeGet(rs: ResultSet, names: Array[String], session: SessionImplementor, owner: Any) = {
val className = StandardBasicTypes.STRING.nullSafeGet(rs, names(0), session, owner).asInstanceOf[String]
val jsonText = StandardBasicTypes.STRING.nullSafeGet(rs, names(1), session, owner).asInstanceOf[String]
new JsonTextObject(className, jsonText)
}
def nullSafeSet(st: PreparedStatement, value: Any, index: Int, session: SessionImplementor) {
val json = value.asInstanceOf[JsonTextObject]
if (json == null) {
StandardBasicTypes.STRING.nullSafeSet(st, null, index, session)
StandardBasicTypes.STRING.nullSafeSet(st, null, index + 1, session)
} else {
StandardBasicTypes.STRING.nullSafeSet(st, json.getClassName, index, session)
StandardBasicTypes.STRING.nullSafeSet(st, json.getJsonText, index + 1, session)
}
}
def deepCopy(value: Any): AnyRef = value.asInstanceOf[AnyRef]
def isMutable = true
def disassemble(value: Any): Serializable = deepCopy(value).asInstanceOf[Serializable]
def assemble(cached: Serializable, owner: Any): AnyRef = deepCopy(cached)
def replace(original: Any, target: Any, owner: Any): AnyRef = deepCopy(original)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment