1 package org.hibernate.test.cut; 3 4 import java.io.Serializable ; 5 import java.math.BigDecimal ; 6 import java.sql.PreparedStatement ; 7 import java.sql.ResultSet ; 8 import java.sql.SQLException ; 9 import java.util.Currency ; 10 11 import org.hibernate.Hibernate; 12 import org.hibernate.HibernateException; 13 import org.hibernate.engine.SessionImplementor; 14 import org.hibernate.type.Type; 15 import org.hibernate.usertype.CompositeUserType; 16 17 20 public class MonetoryAmountUserType implements CompositeUserType { 21 22 public String [] getPropertyNames() { 23 return new String [] { "amount", "currency" }; 24 } 25 26 public Type[] getPropertyTypes() { 27 return new Type[] { Hibernate.BIG_DECIMAL, Hibernate.CURRENCY }; 28 } 29 30 public Object getPropertyValue(Object component, int property) throws HibernateException { 31 MonetoryAmount ma = (MonetoryAmount) component; 32 return property==0 ? (Object ) ma.getAmount() : (Object ) ma.getCurrency(); 33 } 34 35 public void setPropertyValue(Object component, int property, Object value) 36 throws HibernateException { 37 MonetoryAmount ma = (MonetoryAmount) component; 38 if ( property==0 ) { 39 ma.setAmount( (BigDecimal ) value ); 40 } 41 else { 42 ma.setCurrency( (Currency ) value ); 43 } 44 } 45 46 public Class returnedClass() { 47 return MonetoryAmount.class; 48 } 49 50 public boolean equals(Object x, Object y) throws HibernateException { 51 if (x==y) return true; 52 if (x==null || y==null) return false; 53 MonetoryAmount mx = (MonetoryAmount) x; 54 MonetoryAmount my = (MonetoryAmount) y; 55 return mx.getAmount().equals( my.getAmount() ) && 56 mx.getCurrency().equals( my.getCurrency() ); 57 } 58 59 public int hashCode(Object x) throws HibernateException { 60 return ( (MonetoryAmount) x ).getAmount().hashCode(); 61 } 62 63 public Object nullSafeGet(ResultSet rs, String [] names, SessionImplementor session, Object owner) 64 throws HibernateException, SQLException { 65 BigDecimal amt = (BigDecimal ) Hibernate.BIG_DECIMAL.nullSafeGet( rs, names[0] ); 66 Currency cur = (Currency ) Hibernate.CURRENCY.nullSafeGet( rs, names[1] ); 67 if (amt==null) return null; 68 return new MonetoryAmount(amt, cur); 69 } 70 71 public void nullSafeSet(PreparedStatement st, Object value, int index, 72 SessionImplementor session) throws HibernateException, SQLException { 73 MonetoryAmount ma = (MonetoryAmount) value; 74 BigDecimal amt = ma == null ? null : ma.getAmount(); 75 Currency cur = ma == null ? null : ma.getCurrency(); 76 Hibernate.BIG_DECIMAL.nullSafeSet(st, amt, index); 77 Hibernate.CURRENCY.nullSafeSet(st, cur, index+1); 78 } 79 80 public Object deepCopy(Object value) throws HibernateException { 81 MonetoryAmount ma = (MonetoryAmount) value; 82 return new MonetoryAmount( ma.getAmount(), ma.getCurrency() ); 83 } 84 85 public boolean isMutable() { 86 return true; 87 } 88 89 public Serializable disassemble(Object value, SessionImplementor session) 90 throws HibernateException { 91 return (Serializable ) deepCopy(value); 92 } 93 94 public Object assemble(Serializable cached, SessionImplementor session, Object owner) 95 throws HibernateException { 96 return deepCopy(cached); 97 } 98 99 public Object replace(Object original, Object target, SessionImplementor session, Object owner) 100 throws HibernateException { 101 return deepCopy(original); } 103 104 } 105 | Popular Tags |