Skip to content

Instantly share code, notes, and snippets.

@debop
Created September 28, 2013 06:49
Show Gist options
  • Select an option

  • Save debop/6739264 to your computer and use it in GitHub Desktop.

Select an option

Save debop/6739264 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 org.joda.time.{DateTimeZone, DateTime}
import java.util.Objects
import org.hibernate.`type`.StandardBasicTypes
/**
* Joda DateTime 과 TimeZone 정보로 나누어서 각기 컬럼에 저장한다.
* @author 배성혁 sunghyouk.bae@gmail.com
* @since 13. 9. 27. 오후 9:27
*/
class JodaDateTimeTZUserType extends UserType {
val SQL_TYPES = Array(Types.TIMESTAMP, Types.VARCHAR)
def sqlTypes(): Array[Int] = SQL_TYPES
def returnedClass(): Class[_] = classOf[DateTime]
def equals(x: scala.Any, y: scala.Any) = Objects.equals(x, y)
def hashCode(x: scala.Any) = Objects.hashCode(x)
def nullSafeGet(rs: ResultSet, names: Array[String], session: SessionImplementor, owner: scala.Any): AnyRef = {
val timestamp = StandardBasicTypes.TIMESTAMP.nullSafeGet(rs, names(0), session, owner)
val timezone = StandardBasicTypes.STRING.nullSafeGet(rs, names(1), session, owner)
if (timestamp == null || timezone == null)
null
else
new DateTime(timestamp, DateTimeZone.forID(timezone.toString))
}
def nullSafeSet(st: PreparedStatement, value: scala.Any, index: Int, session: SessionImplementor) {
val dt = value.asInstanceOf[DateTime]
if (dt == null) {
StandardBasicTypes.TIMESTAMP.nullSafeSet(st, null, index, session)
StandardBasicTypes.STRING.nullSafeSet(st, null, index + 1, session)
} else {
StandardBasicTypes.TIMESTAMP.nullSafeSet(st, dt.toDate, index, session)
StandardBasicTypes.STRING.nullSafeSet(st, dt.getZone.getID, index + 1, session)
}
}
def deepCopy(value: scala.Any): AnyRef = value.asInstanceOf[AnyRef]
def isMutable = true
def disassemble(value: scala.Any): Serializable = deepCopy(value).asInstanceOf[Serializable]
def assemble(cached: Serializable, owner: scala.Any): AnyRef = deepCopy(cached)
def replace(original: scala.Any, target: scala.Any, owner: scala.Any): AnyRef = deepCopy(original)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment