public class MonetaryAmountCompositeUserType implements CompositeUserType {
public String[] getPropertyNames() {
return new String[] { "amount", "currency" };
}
public Type[] getPropertyTypes() {
return new Type[] { Hibernate.BIG_DECIMAL, Hibernate.CURRENCY };
}
public Object getPropertyValue(Object component, int property)
throws HibernateException {
MonetaryAmount monetaryAmount = (MonetaryAmount) component;
if (property == 0)
return monetaryAmount.getAmount();
else
return monetaryAmount.getCurrency();
}
public Object nullSafeGet(ResultSet resultSet, String[] names,
SessionImplementor session, Object owner)
throws HibernateException, SQLException {
BigDecimal value = resultSet.getBigDecimal(names[0]);
if (resultSet.wasNull())
return null;
Currency currency = Currency.getInstance(resultSet.getString(names[1]));
return new MonetaryAmount(value, currency);
}
public void nullSafeSet(PreparedStatement statement, Object value,
int index, SessionImplementor arg3) throws HibernateException,
SQLException {
if (value == null) {
statement.setNull(index, Hibernate.BIG_DECIMAL.sqlType());
statement.setNull(index + 1, Hibernate.CURRENCY.sqlType());
} else {
MonetaryAmount amount = (MonetaryAmount) value;
String currencyCode = amount.getCurrency().getCurrencyCode();
statement.setBigDecimal(index, amount.getAmount());
statement.setString(index + 1, currencyCode);
}
}
public void setPropertyValue(Object arg0, int arg1, Object arg2)
throws HibernateException {
throw new UnsupportedOperationException("Immutable MonetaryAmount!");
}
}