Class JodaMoneyType

java.lang.Object
google.registry.persistence.converter.JodaMoneyType
All Implemented Interfaces:
org.hibernate.metamodel.spi.EmbeddableInstantiator, org.hibernate.metamodel.spi.Instantiator, org.hibernate.usertype.CompositeUserType<org.joda.money.Money>

public class JodaMoneyType extends Object implements org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
Defines JPA mapping for Joda Money type.

Money is mapped to two table columns, a text currency column that stores the currency code, and a numeric amount column that stores the amount.

The main purpose of this class is to normalize the amount loaded from the database. To support all currency types, the scale of the numeric column is set to 2. As a result, the BigDecimal instances obtained from query ResultSets all have their scale at 2. However, some currency types, e.g., JPY requires that the scale be zero. This class strips trailing zeros from each loaded BigDecimal, then calls the appropriate factory method for Money, which will adjust the scale appropriately.

Conversion of Money is automatic. See NomulusPostgreSQLDialect for more information.

Usage:


 @AttributeOverride(
     name = "amount",
     // Override default (numeric(38,2)) to match real schema definition (numeric(19,2)).
     column = @Column(name = "cost_currency", precision = 19, scale = 2))
 @AttributeOverride(name = "currency", column = @Column(name = "cost_currency"))
 Money cost;
 
  • Constructor Details

    • JodaMoneyType

      public JodaMoneyType()
  • Method Details

    • getPropertyValue

      public Object getPropertyValue(org.joda.money.Money money, int property) throws org.hibernate.HibernateException
      Specified by:
      getPropertyValue in interface org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
      Throws:
      org.hibernate.HibernateException
    • returnedClass

      public Class returnedClass()
      Specified by:
      returnedClass in interface org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
    • equals

      public boolean equals(org.joda.money.Money x, org.joda.money.Money y)
      Specified by:
      equals in interface org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
    • hashCode

      public int hashCode(org.joda.money.Money x) throws org.hibernate.HibernateException
      Specified by:
      hashCode in interface org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
      Throws:
      org.hibernate.HibernateException
    • deepCopy

      public org.joda.money.Money deepCopy(org.joda.money.Money value) throws org.hibernate.HibernateException
      Specified by:
      deepCopy in interface org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
      Throws:
      org.hibernate.HibernateException
    • isMutable

      public boolean isMutable()
      Specified by:
      isMutable in interface org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
    • embeddable

      public Class<?> embeddable()
      Specified by:
      embeddable in interface org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
    • instantiate

      public org.joda.money.Money instantiate(org.hibernate.metamodel.spi.ValueAccess values, org.hibernate.engine.spi.SessionFactoryImplementor sessionFactory)
      Specified by:
      instantiate in interface org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
      Specified by:
      instantiate in interface org.hibernate.metamodel.spi.EmbeddableInstantiator
    • disassemble

      public Serializable disassemble(org.joda.money.Money value) throws org.hibernate.HibernateException
      Specified by:
      disassemble in interface org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
      Throws:
      org.hibernate.HibernateException
    • assemble

      public org.joda.money.Money assemble(Serializable cached, Object owner) throws org.hibernate.HibernateException
      Specified by:
      assemble in interface org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
      Throws:
      org.hibernate.HibernateException
    • replace

      public org.joda.money.Money replace(org.joda.money.Money original, org.joda.money.Money target, Object owner) throws org.hibernate.HibernateException
      Specified by:
      replace in interface org.hibernate.usertype.CompositeUserType<org.joda.money.Money>
      Throws:
      org.hibernate.HibernateException