1 package org.hibernate.type; 3 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.sql.Timestamp ; 8 import java.sql.Types ; 9 import java.text.ParseException ; 10 import java.text.SimpleDateFormat ; 11 import java.util.Comparator ; 12 13 import org.hibernate.EntityMode; 14 import org.hibernate.HibernateException; 15 import org.hibernate.cfg.Environment; 16 import org.hibernate.util.ComparableComparator; 17 18 23 public class TimestampType extends MutableType implements VersionType, LiteralType { 24 25 private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss"; 26 27 public Object get(ResultSet rs, String name) throws SQLException { 28 return rs.getTimestamp(name); 29 } 30 31 public Class getReturnedClass() { 32 return java.util.Date .class; 33 } 34 35 public void set(PreparedStatement st, Object value, int index) throws SQLException { 36 Timestamp ts; 37 if (value instanceof Timestamp ) { 38 ts = (Timestamp ) value; 39 } 40 else { 41 ts = new Timestamp ( ( (java.util.Date ) value ).getTime() ); 42 } 43 st.setTimestamp(index, ts); 44 } 45 46 public int sqlType() { 47 return Types.TIMESTAMP; 48 } 49 50 public String getName() { return "timestamp"; } 51 52 public String toString(Object val) { 53 return new SimpleDateFormat (TIMESTAMP_FORMAT).format( (java.util.Date ) val ); 54 } 55 56 public Object deepCopyNotNull(Object value) { 57 if ( value instanceof Timestamp ) { 58 Timestamp orig = (Timestamp ) value; 59 Timestamp ts = new Timestamp ( orig.getTime() ); 60 ts.setNanos( orig.getNanos() ); 61 return ts; 62 } 63 else { 64 java.util.Date orig = (java.util.Date ) value; 65 return new java.util.Date ( orig.getTime() ); 66 } 67 } 68 69 public boolean isEqual(Object x, Object y) { 70 71 if (x==y) return true; 72 if (x==null || y==null) return false; 73 74 long xTime = ( (java.util.Date ) x ).getTime(); 75 long yTime = ( (java.util.Date ) y ).getTime(); 76 boolean xts = x instanceof Timestamp ; 77 boolean yts = y instanceof Timestamp ; 78 int xNanos = xts ? ( (Timestamp ) x ).getNanos() : 0; 79 int yNanos = yts ? ( (Timestamp ) y ).getNanos() : 0; 80 if ( !Environment.jvmHasJDK14Timestamp() ) { 81 xTime += xNanos / 1000000; 82 yTime += yNanos / 1000000; 83 } 84 if ( xTime!=yTime ) return false; 85 if (xts && yts) { 86 int xn = xNanos % 1000000; 88 int yn = yNanos % 1000000; 89 return xn==yn; 90 } 91 else { 92 return true; 94 } 95 96 } 97 98 public int getHashCode(Object x, EntityMode entityMode) { 99 java.util.Date ts = (java.util.Date ) x; 100 return new Long ( ts.getTime() / 1000 ).hashCode(); 101 } 102 103 public Object next(Object current) { 104 return seed(); 105 } 106 107 public Object seed() { 108 return new Timestamp ( System.currentTimeMillis() ); 109 } 110 111 public Comparator getComparator() { 112 return ComparableComparator.INSTANCE; 113 } 114 115 public String objectToSQLString(Object value) throws Exception { 116 return '\'' + value.toString() + '\''; 117 } 118 119 public Object fromStringValue(String xml) throws HibernateException { 120 try { 121 return new Timestamp ( new SimpleDateFormat (TIMESTAMP_FORMAT).parse(xml).getTime() ); 122 } 123 catch (ParseException pe) { 124 throw new HibernateException("could not parse XML", pe); 125 } 126 } 127 128 } 129 130 131 132 133 134 | Popular Tags |