SHARE
TWEET

Untitled

a guest Apr 8th, 2015 399 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.hibernate.HibernateException;
  2. import org.hibernate.dialect.Dialect;
  3. import org.hibernate.engine.spi.SessionImplementor;
  4. import org.hibernate.type.AbstractSingleColumnStandardBasicType;
  5. import org.hibernate.type.LiteralType;
  6. import org.hibernate.type.TimestampType;
  7. import org.hibernate.type.VersionType;
  8. import org.hibernate.type.descriptor.ValueBinder;
  9. import org.hibernate.type.descriptor.ValueExtractor;
  10. import org.hibernate.type.descriptor.WrapperOptions;
  11. import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
  12. import org.hibernate.type.descriptor.java.JdbcTimestampTypeDescriptor;
  13. import org.hibernate.type.descriptor.sql.BasicBinder;
  14. import org.hibernate.type.descriptor.sql.BasicExtractor;
  15. import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
  16.  
  17. import java.sql.*;
  18. import java.util.Calendar;
  19. import java.util.Comparator;
  20. import java.util.Date;
  21. import java.util.TimeZone;
  22.  
  23. public class UTCTimestampType extends AbstractSingleColumnStandardBasicType<Date>
  24.         implements VersionType<Date>, LiteralType<Date> {
  25.  
  26.         public static final UTCTimestampType INSTANCE = new UTCTimestampType();
  27.  
  28.         public static class UtcTimestampTypeDescriptor extends TimestampTypeDescriptor {
  29.                 public static final UtcTimestampTypeDescriptor INSTANCE = new UtcTimestampTypeDescriptor();
  30.  
  31.                 private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
  32.  
  33.                 public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
  34.                         return new BasicBinder<X>(javaTypeDescriptor, this) {
  35.                                 @Override
  36.                                 protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
  37.                                         st.setTimestamp(index, javaTypeDescriptor.unwrap(value, Timestamp.class, options), Calendar.getInstance(UTC));
  38.                                 }
  39.                         };
  40.                 }
  41.  
  42.                 public <X> ValueExtractor<X> getExtractor(final JavaTypeDescriptor<X> javaTypeDescriptor) {
  43.                         return new BasicExtractor<X>(javaTypeDescriptor, this) {
  44.  
  45.                                 @Override
  46.                                 protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
  47.                                         return javaTypeDescriptor.wrap(statement.getTimestamp(name, Calendar.getInstance(UTC)), options);
  48.                                 }
  49.  
  50.                                 @Override
  51.                                 protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
  52.                                         return javaTypeDescriptor.wrap(rs.getTimestamp(name, Calendar.getInstance(UTC)), options);
  53.                                 }
  54.  
  55.                                 @Override
  56.                                 protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
  57.                                         return javaTypeDescriptor.wrap(statement.getTimestamp(index, Calendar.getInstance(UTC)), options);
  58.                                 }
  59.  
  60.                         };
  61.                 }
  62.         }
  63.  
  64.  
  65.         public UTCTimestampType() {
  66.                 super(UtcTimestampTypeDescriptor.INSTANCE, JdbcTimestampTypeDescriptor.INSTANCE);
  67.         }
  68.  
  69.         public String getName() {
  70.                 return TimestampType.INSTANCE.getName();
  71.         }
  72.  
  73.         @Override
  74.         public String[] getRegistrationKeys() {
  75.                 return TimestampType.INSTANCE.getRegistrationKeys();
  76.         }
  77.  
  78.         public Date next(Date current, SessionImplementor session) {
  79.                 return TimestampType.INSTANCE.next(current, session);
  80.         }
  81.  
  82.         public Date seed(SessionImplementor session) {
  83.                 return TimestampType.INSTANCE.seed(session);
  84.         }
  85.  
  86.         public Comparator<Date> getComparator() {
  87.                 return TimestampType.INSTANCE.getComparator();
  88.         }
  89.  
  90.         public String objectToSQLString(Date value, Dialect dialect) throws Exception {
  91.                 return TimestampType.INSTANCE.objectToSQLString(value, dialect);
  92.         }
  93.  
  94.         public Date fromStringValue(String xml) throws HibernateException {
  95.                 return TimestampType.INSTANCE.fromStringValue(xml);
  96.         }
  97.  
  98. }
  99.  
  100. /** --------------------------------------------------------------------------------------------------- **/
  101.  
  102. import org.hibernate.SessionFactory;
  103. import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
  104.  
  105. public class LocalSessionFactoryBean extends org.springframework.orm.hibernate4.LocalSessionFactoryBean {
  106.  
  107.         @Override
  108.         protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
  109.                 sfb.registerTypeOverride(new UTCTimestampType());
  110.  
  111.                 return super.buildSessionFactory(sfb);
  112.         }
  113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top