如何在mysql中存储joda.money [英] How to store joda.money in mysql

查看:339
本文介绍了如何在mysql中存储joda.money的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有数据类型joda.money属性的类。
如何将这个属性映射到mysql中。

我的类是:
$ b


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

我使用spring hibernate



任何人都可以告诉如何存储这种模式转换为mysql?

解决方案

您在这里有更多的选择。


  1. 您可以让两个字段代表Joda的 Money BigDecimal 用于金额,字符串用于货币。然后创建瞬态综合字段 Money 并在getter中创建它(请参见下面的示例)。

  2. 您可以实现自己的 UserType ,并提供您自己的序列化/反序列化。请参阅javadoc UserType 和文档使用org.hibernate.usertype.UserType的自定义类型。看看这个实现 CompositeUserType

  3. Usertypes项目,为Hibernate提供Joda Money 支持

      @Entity @Table(name =products)
    public class Product {
    @Basic @Column
    private BigDecimal amount;

    @Basic @Column
    私人字符串货币;

    私人短暂金钱;
    $ b public Money getMoney(){
    if(money == null){
    money = Money.of(CurrencyUnit.of(currency),amount);
    }

    回报资金;
    }
    }



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;
            }
    }
    

这篇关于如何在mysql中存储joda.money的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆