1 package org.hibernate.test.typeparameters; 2 3 import java.io.Serializable ; 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.sql.Types ; 8 import java.util.Properties ; 9 10 import org.apache.commons.logging.LogFactory; 11 import org.hibernate.HibernateException; 12 import org.hibernate.usertype.ParameterizedType; 13 import org.hibernate.usertype.UserType; 14 15 16 19 public class DefaultValueIntegerType implements UserType, ParameterizedType, Serializable { 20 21 private Integer defaultValue; 22 23 public int[] sqlTypes() { 24 return new int[] {Types.INTEGER}; 25 } 26 27 public Class returnedClass() { 28 return int.class; 29 } 30 31 public boolean equals(Object x, Object y) throws HibernateException { 32 if (x==y) return true; 33 if (x==null || y==null) return false; 34 return x.equals(y); 35 } 36 37 public Object nullSafeGet(ResultSet rs, String [] names, Object owner) throws HibernateException, SQLException { 38 Number result = (Number ) rs.getObject(names[0]); 39 return result==null ? defaultValue : new Integer (result.intValue()); 40 } 41 42 public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { 43 if (value == null || defaultValue.equals(value) ) { 44 LogFactory.getLog( getClass() ).trace("binding null to parameter: " + index); 45 st.setNull(index, Types.INTEGER); 46 } else { 47 LogFactory.getLog( getClass() ).trace("binding " + value + " to parameter: " + index); 48 st.setInt(index, ((Integer )value).intValue()); 49 } 50 } 51 52 public Object deepCopy(Object value) throws HibernateException { 53 return new Integer (((Integer )value).intValue()); 54 } 55 56 public boolean isMutable() { 57 return false; 58 } 59 60 public int hashCode(Object x) throws HibernateException { 61 return x.hashCode(); 62 } 63 64 public Object assemble(Serializable cached, Object owner) 65 throws HibernateException { 66 return cached; 67 } 68 69 public Serializable disassemble(Object value) throws HibernateException { 70 return (Serializable ) value; 71 } 72 73 public Object replace(Object original, Object target, Object owner) 74 throws HibernateException { 75 return original; 76 } 77 78 public void setParameterValues(Properties parameters) { 79 this.defaultValue = Integer.valueOf((String ) parameters.get("default")); 80 } 81 82 } 83 | Popular Tags |