本文介绍了如何在mysql中存储joda.money的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有数据类型joda.money属性的类。
如何将这个属性映射到mysql中。
我的类是:
$ b
我使用spring hibernate
任何人都可以告诉如何存储这种模式转换为mysql?
解决方案
您在这里有更多的选择。
- 您可以让两个字段代表Joda的
Money
。BigDecimal
用于金额,字符串
用于货币。然后创建瞬态综合字段Money
并在getter中创建它(请参见下面的示例)。 - 您可以实现自己的
UserType
,并提供您自己的序列化/反序列化。请参阅javadoc 和文档。看看实现CompositeUserType
。 -
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:
I am using spring hibernate
Can anyone tell how to store this model into mysql?
解决方案
You have more choices here.
- You can make two fields representing Joda's
Money
.BigDecimal
for amount andString
for currency. Then create transient synthetic fieldMoney
and create it in getter (see example below). - 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 ofCompositeUserType
. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!