1 package org.hibernate.ce.auction.persistence; 2 3 import org.hibernate.*; 4 import org.hibernate.usertype.UserType; 5 import org.hibernate.ce.auction.model.*; 6 7 import java.math.BigDecimal ; 8 import java.sql.*; 9 import java.util.Currency ; 10 import java.io.Serializable ; 11 12 22 public class MonetaryAmountSimpleUserType 23 implements UserType { 24 25 private static final int[] SQL_TYPES = {Types.NUMERIC}; 26 27 public int[] sqlTypes() { return SQL_TYPES; } 28 29 public Class returnedClass() { return MonetaryAmount.class; } 30 31 public boolean isMutable() { return false; } 32 33 public Object deepCopy(Object value) { 34 return value; } 36 37 public boolean equals(Object x, Object y) { 38 if (x == y) return true; 39 if (x == null || y == null) return false; 40 return x.equals(y); 41 } 42 43 public int hashCode(Object o) throws HibernateException { return o.hashCode(); } 44 public Serializable disassemble(Object o) throws HibernateException { return (Serializable )o; } 45 public Object assemble(Serializable cached, Object owner) throws HibernateException { return cached; } 46 public Object replace(Object o, Object o1, Object o2) throws HibernateException { return null; } 48 49 public Object nullSafeGet(ResultSet resultSet, 50 String [] names, 51 Object owner) 52 throws HibernateException, SQLException { 53 54 if (resultSet.wasNull()) return null; 55 BigDecimal valueInUSD = resultSet.getBigDecimal(names[0]); 56 Currency userCurrency = Currency.getInstance("USD"); 57 return new MonetaryAmount(valueInUSD, userCurrency); 58 } 59 60 public void nullSafeSet(PreparedStatement statement, 61 Object value, 62 int index) 63 throws HibernateException, SQLException { 64 65 if (value == null) { 66 statement.setNull(index, Types.NUMERIC); 67 } else { 68 MonetaryAmount anyCurrency = (MonetaryAmount)value; 69 MonetaryAmount amountInUSD = 70 MonetaryAmount.convert( anyCurrency, 71 Currency.getInstance("USD") ); 72 statement.setBigDecimal(index, amountInUSD.getValue()); 73 } 74 } 75 } 76 | Popular Tags |