문제

I have a class which has attribute of datatype joda.money. How can I map this attribute into mysql.

my class is:

@Table(name="products") public class Product(){ @Column(name="name") String name; @Column(name="money") Money money; }

I am using spring hibernate

Can anyone tell how to store this model into mysql?

도움이 되었습니까?

해결책

You have more choices here.

  1. You can make two fields representing Joda's Money. BigDecimal for amount and String for currency. Then create transient synthetic field Money and create it in getter (see example below).
  2. You can implement your own UserType and provide serialization/deserialization by your own. See javadoc UserType and documenatation Custom types using org.hibernate.usertype.UserType. Take a look at this implementation of CompositeUserType.
  3. You can include Jadira Usertypes project which provides Joda Money support for Hibernate (I've never tryied anyway)

    @Entity @Table(name = "products")
    public class Product {
        @Basic @Column
        private BigDecimal amount;
    
        @Basic @Column
        private String currency;
    
        private transient Money money;
    
            public Money getMoney() {
                if (money == null) {
                    money = Money.of(CurrencyUnit.of(currency), amount);
                }
    
                return money;
            }
    }
    
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top