Hibernateの永続性を提供するJodaTime用のライブラリがあります。最近、私はJoda-Moneyを調べ始め、休止状態を使用してそれをどのように維持できるかを確認し始めましたが、ライブラリは表示されません。
助言がありますか?
Hibernateの永続性を提供するJodaTime用のライブラリがあります。最近、私はJoda-Moneyを調べ始め、休止状態を使用してそれをどのように維持できるかを確認し始めましたが、ライブラリは表示されません。
助言がありますか?
Sudarshanの回答の例へのリンクが壊れているため、ここに、の単純なカスタムユーザータイプの実装がありますorg.joda.money.BigMoney
。これは、金額と通貨の2つの列にmoneyオブジェクトを保持します)とその使用方法の例です。についても同じように機能しorg.joda.money.Money
ます。
package test;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Currency;
import org.apache.commons.lang.ObjectUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import org.hibernate.usertype.CompositeUserType;
import org.joda.money.BigMoney;
import org.joda.money.CurrencyUnit;
public class MoneyUserType implements CompositeUserType
{
private static final String[] PROPERTY_NAMES = {"amount", "currencyUnit"};
private static final Type[] PROPERTY_TYPES = {StandardBasicTypes.BIG_DECIMAL, StandardBasicTypes.CURRENCY};
public MoneyUserType()
{
super();
}
public Object assemble(final Serializable cached, final SessionImplementor session, final Object owner)
throws HibernateException
{
return cached;
}
public Serializable disassemble(final Object value, final SessionImplementor session) throws HibernateException
{
return (Serializable) value;
}
public String[] getPropertyNames()
{
return PROPERTY_NAMES.clone();
}
public Type[] getPropertyTypes()
{
return PROPERTY_TYPES.clone();
}
public Object getPropertyValue(final Object component, final int property) throws HibernateException
{
BigMoney money = (BigMoney) component;
return (property == 0) ? money.getAmount() : money.getCurrencyUnit().toCurrency();
}
public Object nullSafeGet(final ResultSet rs, final String[] names, final SessionImplementor session,
final Object owner) throws HibernateException, SQLException
{
BigDecimal amount = StandardBasicTypes.BIG_DECIMAL.nullSafeGet(rs, names[0], session);
Currency currency = StandardBasicTypes.CURRENCY.nullSafeGet(rs, names[1], session);
return BigMoney.of(CurrencyUnit.of(currency), amount);
}
public void nullSafeSet(final PreparedStatement st, final Object value, final int index,
final SessionImplementor session) throws HibernateException, SQLException
{
BigMoney money = (BigMoney) value;
BigDecimal amount = (money == null) ? null : money.getAmount();
Currency currency = (money == null) ? null : money.getCurrencyUnit().toCurrency();
StandardBasicTypes.BIG_DECIMAL.nullSafeSet(st, amount, index, session);
StandardBasicTypes.CURRENCY.nullSafeSet(st, currency, index + 1, session);
}
public Object replace(final Object original, final Object target, final SessionImplementor session,
final Object owner) throws HibernateException
{
return deepCopy(original);
}
public void setPropertyValue(final Object component, final int property, final Object value)
throws HibernateException
{
throw new HibernateException("Money is immutable.");
}
public Object deepCopy(final Object value) throws HibernateException
{
return (value != null) ? BigMoney.of(((BigMoney) value).getCurrencyUnit(),
((BigMoney) value).getAmount()) : null;
}
public boolean equals(final Object x, final Object y) throws HibernateException
{
return ObjectUtils.equals(x, y);
}
public int hashCode(final Object x) throws HibernateException
{
return ObjectUtils.hashCode(x);
}
public boolean isMutable()
{
return false;
}
public Class<?> returnedClass()
{
return BigMoney.class;
}
}
使用法:
@Type(type = "test.MoneyUserType")
@Columns(columns = {@Column(name = "AMOUNT"), @Column(name = "CURRENCY")})
private BigMoney money;
User Typeプロジェクトには、JodaMoneyのサポートが含まれています。
User Typeプロジェクトは、バージョン3.0.0以降のjoda-money0.6のサポートを提供します。ただし、これにはHibernate4が必要であることに注意してください。また、現在のjoda-moneyバージョンは0.8です。
Hibernate 3で使用する場合は、Sudarshan anwserの例を使用してください(執筆時点ではバグがあります)。
http://jadira.sourceforge.netに基づく
お金の種類は通常、通貨と金額で構成されます。Jadiraを使用すると、パラメーターを使用して構成された通貨を使用して、金額のみをデータベースに保存できます。例えば:
@Column
@Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmount",
parameters = {@org.hibernate.annotations.Parameter(name = "currencyCode", value = "USD")})
private Money money;
Alternatively, with other types two columns to hold the amount an currency:
@Columns(columns = { @Column(name = "MY_CURRENCY"), @Column(name = "MY_AMOUNT") })
@Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmountAndCurrency")
private Money money;
Joda-Moneyは非常に新しいため、Hibernateマッピングをまだ提供している人がいないのも当然です。
ただし、カスタムHibernateタイプのアダプターを作成するのは非常に簡単です。JodaTimeアダプターのソースを見ると、それらが本当に単純であることがわかります。独自の記述方法については、ドキュメントを参照してください。