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.Time ; 8 import java.sql.Types ; 9 import java.text.ParseException ; 10 import java.text.SimpleDateFormat ; 11 import java.util.Calendar ; 12 import java.util.Date ; 13 14 import org.hibernate.EntityMode; 15 import org.hibernate.HibernateException; 16 17 22 public class TimeType extends MutableType implements LiteralType { 23 24 private static final String TIME_FORMAT = "HH:mm:ss"; 25 26 public Object get(ResultSet rs, String name) throws SQLException { 27 return rs.getTime(name); 28 } 29 public Class getReturnedClass() { 30 return java.util.Date .class; 31 } 32 public void set(PreparedStatement st, Object value, int index) throws SQLException { 33 34 Time time; 35 if (value instanceof Time ) { 36 time = (Time ) value; 37 } 38 else { 39 time = new Time ( ( (java.util.Date ) value ).getTime() ); 40 } 41 st.setTime(index, time); 42 } 43 44 public int sqlType() { 45 return Types.TIME; 46 } 47 public String getName() { return "time"; } 48 49 public String toString(Object val) { 50 return new SimpleDateFormat (TIME_FORMAT).format( (java.util.Date ) val ); 51 } 52 public boolean isEqual(Object x, Object y) { 53 54 if (x==y) return true; 55 if (x==null || y==null) return false; 56 57 Date xdate = (Date ) x; 58 Date ydate = (Date ) y; 59 60 if ( xdate.getTime()==ydate.getTime() ) return true; 61 62 Calendar calendar1 = java.util.Calendar.getInstance(); 63 Calendar calendar2 = java.util.Calendar.getInstance(); 64 calendar1.setTime( xdate ); 65 calendar2.setTime( ydate ); 66 67 return calendar1.get(Calendar.HOUR_OF_DAY) == calendar2.get(Calendar.HOUR_OF_DAY) 68 && calendar1.get(Calendar.MINUTE) == calendar2.get(Calendar.MINUTE) 69 && calendar1.get(Calendar.SECOND) == calendar2.get(Calendar.SECOND) 70 && calendar1.get(Calendar.MILLISECOND) == calendar2.get(Calendar.MILLISECOND); 71 } 72 73 public int getHashCode(Object x, EntityMode entityMode) { 74 Calendar calendar = java.util.Calendar.getInstance(); 75 calendar.setTime( (java.util.Date ) x ); 76 int hashCode = 1; 77 hashCode = 31 * hashCode + calendar.get(Calendar.HOUR_OF_DAY); 78 hashCode = 31 * hashCode + calendar.get(Calendar.MINUTE); 79 hashCode = 31 * hashCode + calendar.get(Calendar.SECOND); 80 hashCode = 31 * hashCode + calendar.get(Calendar.MILLISECOND); 81 return hashCode; 82 } 83 84 public Object deepCopyNotNull(Object value) { 85 return new Time ( ( (java.util.Date ) value ).getTime() ); 86 } 87 88 public String objectToSQLString(Object value) throws Exception { 89 return '\'' + value.toString() + '\''; 90 } 91 92 public Object fromStringValue(String xml) throws HibernateException { 93 try { 94 return new SimpleDateFormat (TIME_FORMAT).parse(xml); 95 } 96 catch (ParseException pe) { 97 throw new HibernateException("could not parse XML", pe); 98 } 99 } 100 101 } 102 103 104 105 106 107 | Popular Tags |