KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > persistence > MonetaryAmountCompositeUserType


1 package org.hibernate.ce.auction.persistence;
2
3 import org.hibernate.*;
4 import org.hibernate.usertype.CompositeUserType;
5 import org.hibernate.engine.SessionImplementor;
6 import org.hibernate.type.Type;
7 import org.hibernate.ce.auction.model.MonetaryAmount;
8
9 import java.io.Serializable JavaDoc;
10 import java.math.BigDecimal JavaDoc;
11 import java.sql.*;
12 import java.util.Currency JavaDoc;
13
14 /**
15  * This is a simple Hibernate custom mapping type for MonetaryAmount value types.
16  * <p>
17  * Basically the same as the simple <tt>MonetaryAmountSimpleUserType</tt>, but
18  * implementing the Hibernate <tt>CompositeUserType</tt> interface. This interface
19  * has some additional methods that allow Hibernate to analyze the value type you
20  * are mapping. This is mostly useful for HQL queries: with this custom mapping
21  * type, you can use the "amount" and "currency" sub-components in HQL queries.
22  *
23  * @see MonetaryAmountSimpleUserType
24  * @author Christian Bauer <christian@hibernate.org>
25  */

26 public class MonetaryAmountCompositeUserType
27         implements CompositeUserType {
28
29     public Class JavaDoc returnedClass() { return MonetaryAmount.class; }
30
31     public boolean equals(Object JavaDoc x, Object JavaDoc y) {
32         if (x == y) return true;
33         if (x == null || y == null) return false;
34         return x.equals(y);
35     }
36     public int hashCode(Object JavaDoc o) throws HibernateException { return o.hashCode(); }
37     public Serializable JavaDoc disassemble(Object JavaDoc o, SessionImplementor s) throws HibernateException { return (Serializable JavaDoc)o; }
38     public Object JavaDoc assemble(Serializable JavaDoc cached, SessionImplementor s, Object JavaDoc owner) throws HibernateException { return cached; }
39     // TODO: Whats this?
40
public Object JavaDoc replace(Object JavaDoc o, Object JavaDoc o1, SessionImplementor s, Object JavaDoc o2) throws HibernateException { return null; }
41
42     public Object JavaDoc deepCopy(Object JavaDoc value) {
43         return value; // MonetaryAmount is immutable
44
}
45
46     public boolean isMutable() { return false; }
47
48     public Object JavaDoc nullSafeGet(ResultSet resultSet,
49                               String JavaDoc[] names,
50                               SessionImplementor session,
51                               Object JavaDoc owner)
52             throws HibernateException, SQLException {
53
54         if (resultSet.wasNull()) return null;
55         BigDecimal JavaDoc value = resultSet.getBigDecimal( names[0] );
56         Currency JavaDoc currency =
57             Currency.getInstance(resultSet.getString( names[1] ) );
58         return new MonetaryAmount(value, currency);
59     }
60
61     public void nullSafeSet(PreparedStatement statement,
62                             Object JavaDoc value,
63                             int index,
64                             SessionImplementor session)
65             throws HibernateException, SQLException {
66
67         if (value==null) {
68             statement.setNull(index, Types.NUMERIC);
69             statement.setNull(index+1, Types.VARCHAR);
70         } else {
71             MonetaryAmount amount = (MonetaryAmount) value;
72             String JavaDoc currencyCode =
73                         amount.getCurrency().getCurrencyCode();
74             statement.setBigDecimal( index, amount.getValue() );
75             statement.setString( index+1, currencyCode );
76         }
77     }
78
79     public String JavaDoc[] getPropertyNames() {
80         return new String JavaDoc[] { "value", "currency" };
81     }
82
83     public Type[] getPropertyTypes() {
84         return new Type[] { Hibernate.BIG_DECIMAL, Hibernate.CURRENCY };
85     }
86
87     public Object JavaDoc getPropertyValue(Object JavaDoc component,
88                                    int property)
89             throws HibernateException {
90         MonetaryAmount monetaryAmount = (MonetaryAmount) component;
91         if (property == 0)
92             return monetaryAmount.getValue();
93         else
94             return monetaryAmount.getCurrency();
95     }
96
97     public void setPropertyValue(Object JavaDoc component,
98                                  int property,
99                                  Object JavaDoc value) throws HibernateException {
100        throw new UnsupportedOperationException JavaDoc("MonetaryAmount is immutable");
101     }
102
103 }
Popular Tags