1 package org.hibernate.ce.auction.persistence; 2 3 import org.hibernate.usertype.*; 4 import org.hibernate.ce.auction.model.MonetaryAmount; 5 import org.hibernate.*; 6 import org.hibernate.engine.SessionImplementor; 7 8 import java.util.*; 9 import java.sql.*; 10 import java.math.BigDecimal ; 11 12 public class MonetaryAmountType extends MonetaryAmountCompositeUserType implements ParameterizedType { 21 22 private Currency convertTo; 24 25 public void setParameterValues(Properties parameters) { 26 this.convertTo = Currency.getInstance(parameters.getProperty("convertTo")); 27 } 28 29 public Object nullSafeGet(ResultSet resultSet, 30 String [] names, 31 SessionImplementor session, 32 Object owner) 33 throws HibernateException, SQLException { 34 35 if (resultSet.wasNull()) return null; 36 BigDecimal value = resultSet.getBigDecimal( names[0] ); 37 Currency currency = Currency.getInstance(resultSet.getString( names[1] ) ); 39 return new MonetaryAmount(value, currency); 40 } 41 42 public void nullSafeSet(PreparedStatement statement, 43 Object value, 44 int index, 45 SessionImplementor session) 46 throws HibernateException, SQLException { 47 48 if (value==null) { 49 statement.setNull(index, Types.NUMERIC); 50 } else { 51 MonetaryAmount amount = (MonetaryAmount) value; 52 MonetaryAmount dbAmount = MonetaryAmount.convert(amount, convertTo); 54 statement.setBigDecimal( index, dbAmount.getValue() ); 55 statement.setString( index+1, dbAmount.getCurrency().getCurrencyCode()); 56 } 57 } 58 59 } | Popular Tags |