KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > sql > MonetaryAmountUserType


1 package org.hibernate.test.sql;
2
3 import java.io.Serializable JavaDoc;
4 import java.math.BigDecimal JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.sql.Types JavaDoc;
9 import java.util.Currency JavaDoc;
10
11 import org.hibernate.HibernateException;
12 import org.hibernate.usertype.UserType;
13
14 /**
15  * This is a simple Hibernate custom mapping type for MonetaryAmount value types.
16  * <p>
17  *
18  * @author Max & Christian
19  */

20 public class MonetaryAmountUserType
21         implements UserType {
22
23     private static final int[] SQL_TYPES = {Types.NUMERIC, Types.VARCHAR };
24
25     public int[] sqlTypes() { return SQL_TYPES; }
26
27     public Class JavaDoc returnedClass() { return MonetaryAmount.class; }
28
29     public boolean isMutable() { return false; }
30
31     public Object JavaDoc deepCopy(Object JavaDoc value) {
32         return value; // MonetaryAmount is immutable
33
}
34
35     public boolean equals(Object JavaDoc x, Object JavaDoc y) {
36         if (x == y) return true;
37         if (x == null || y == null) return false;
38         return x.equals(y);
39     }
40
41     public Object JavaDoc nullSafeGet(ResultSet JavaDoc resultSet,
42                               String JavaDoc[] names,
43                               Object JavaDoc owner)
44             throws HibernateException, SQLException JavaDoc {
45
46         BigDecimal JavaDoc value = resultSet.getBigDecimal(names[0]);
47         if (resultSet.wasNull()) return null;
48         String JavaDoc cur = resultSet.getString(names[1]);
49         Currency JavaDoc userCurrency = Currency.getInstance(cur);
50                         
51         return new MonetaryAmount(value, userCurrency);
52     }
53
54     public void nullSafeSet(PreparedStatement JavaDoc statement,
55                             Object JavaDoc value,
56                             int index)
57             throws HibernateException, SQLException JavaDoc {
58
59         if (value == null) {
60             statement.setNull(index, Types.NUMERIC);
61             statement.setNull(index+1, Types.VARCHAR);
62         } else {
63             MonetaryAmount currency = (MonetaryAmount)value;
64             statement.setBigDecimal(index, currency.getValue());
65             statement.setString(index+1, currency.getCurrency().getCurrencyCode());
66         }
67     }
68
69     public Serializable JavaDoc disassemble(Object JavaDoc value) throws HibernateException {
70         return (Serializable JavaDoc) value;
71     }
72
73     public Object JavaDoc assemble(Serializable JavaDoc cached, Object JavaDoc owner) throws HibernateException {
74         return cached;
75     }
76
77     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner)
78     throws HibernateException {
79         return original;
80     }
81
82     public int hashCode(Object JavaDoc x) throws HibernateException {
83         return x.hashCode();
84     }
85 }
86
Popular Tags