Search code examples
javahibernatejoda-money

How to map Joda Money with org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmountAndCurrency type in Hibernate?


Trying this:

@Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmountAndCurrency")
private org.joda.money.Money price;

Getting this:

org.hibernate.MappingException: property mapping has wrong number of columns:domain.ClientOrderItem.price type: org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmountAndCurrency


@Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmount",
parameters = {@org.hibernate.annotations.Parameter(name = "currencyCode", value = "USD")})

Works nice, but I want to store currency in database and be able to use different currencies.


Solution

  • There's a working example from Jadira Usertype Unit Tests

        @Entity
        @Table(name = "moneyAmountAndCurrency")
        @TypeDef(name = "testjoda_MoneyAmountWithCurrencyType", typeClass = PersistentMoneyAmountAndCurrency.class)
        public class MoneyAmountAndCurrencyHolder implements Serializable {
    
        private static final long serialVersionUID = -1674416082110551506L;
    
        @Columns(columns = { @Column(name = "MY_CURRENCY"), @Column(name = "MY_AMOUNT") })
        @Type(type = "testjoda_MoneyAmountWithCurrencyType")
        private Money money;