Grails and hibernate usually need complete domain classes that can be modified to support all the functions provided by hibernate.
, Money, Money UserType. , UserType:
import java.sql.*
import org.hibernate.usertype.UserType
class MoneyUserType implements UserType {
int[] sqlTypes() {
[Types.VARCHAR, Types.DECIMAL] as int[]
}
Class returnedClass() {
Money
}
def nullSafeGet(ResultSet resultSet, String[] names, Object owner) HibernateException, SQLException {
String currency = resultSet.getString(names[0])
BigDecimal value = resultSet.getBigDecimal(names[1])
if (currency != null && value != null) {
new Money(currency, value)
} else {
new Money("", 0.0)
}
}
void nullSafeSet(PreparedStatement statement, Object money, int index) {
statement.setString(index, money?.currency ?: "")
statement.setBigDecimal(index+1, money?.value ?: 0.0)
}
...
}
, UserType :
class Billing {
static mapping = {
amount type: MoneyUserType
}
Money amount
}