Grails GORM using an immutable built-in object

I have a GORM class that uses a built-in instance in it. And the inline instance is an immutable class. When I try to start the application, it throws a setter property, an exception not found.

Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property amount in class com.xxx.Money.

This is my GORM class:

class Billing {
    static embedded = ['amount']
    Money amount
}

And money is defined as unchanged:

final class Money {
    final Currency currency
    final BigDecimal value

    Money(Currency currency, BigDecimal value) {
        this.currency = currency
        this.value = value
    }
}

In any case, to resolve this without making Money changed?

Thanks!

+4
source share
1 answer

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
}
+1

All Articles