Class JodaMoneyType
- All Implemented Interfaces:
org.hibernate.usertype.CompositeUserType
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.
Although CompositeUserType
is likely to suffer breaking change in Hibernate 6, it is
the only option. The suggested alternatives such as Hibernate component or Java Embeddable do not
work in this case. Hibernate component (our previous solution that is replaced by this class)
does not allow manipulation of the loaded amount objects. Java Embeddable is not applicable since
we do not own the Joda money classes.
Usage:
'@'Type(type = JodaMoneyType.TYPE_NAME)
'@'Columns(
columns = {
'@'Column(name = "cost_amount"),
'@'Column(name = "cost_currency")
}
)
Money cost;
-
Field Summary
Modifier and TypeFieldDescriptionstatic final JodaMoneyType
static final String
The name of this type registered with JPA. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionassemble
(Serializable cached, org.hibernate.engine.spi.SharedSessionContractImplementor session, Object owner) disassemble
(Object value, org.hibernate.engine.spi.SharedSessionContractImplementor session) boolean
String[]
org.hibernate.type.Type[]
getPropertyValue
(Object component, int property) int
boolean
nullSafeGet
(ResultSet rs, String[] names, org.hibernate.engine.spi.SharedSessionContractImplementor session, Object owner) void
nullSafeSet
(PreparedStatement st, Object value, int index, org.hibernate.engine.spi.SharedSessionContractImplementor session) replace
(Object original, Object target, org.hibernate.engine.spi.SharedSessionContractImplementor session, Object owner) void
setPropertyValue
(Object component, int property, Object value)
-
Field Details
-
INSTANCE
-
TYPE_NAME
The name of this type registered with JPA. See the example in class doc.- See Also:
-
-
Constructor Details
-
JodaMoneyType
public JodaMoneyType()
-
-
Method Details
-
getPropertyNames
- Specified by:
getPropertyNames
in interfaceorg.hibernate.usertype.CompositeUserType
-
getPropertyTypes
public org.hibernate.type.Type[] getPropertyTypes()- Specified by:
getPropertyTypes
in interfaceorg.hibernate.usertype.CompositeUserType
-
getPropertyValue
public Object getPropertyValue(Object component, int property) throws org.hibernate.HibernateException - Specified by:
getPropertyValue
in interfaceorg.hibernate.usertype.CompositeUserType
- Throws:
org.hibernate.HibernateException
-
setPropertyValue
public void setPropertyValue(Object component, int property, Object value) throws org.hibernate.HibernateException - Specified by:
setPropertyValue
in interfaceorg.hibernate.usertype.CompositeUserType
- Throws:
org.hibernate.HibernateException
-
returnedClass
- Specified by:
returnedClass
in interfaceorg.hibernate.usertype.CompositeUserType
-
equals
- Specified by:
equals
in interfaceorg.hibernate.usertype.CompositeUserType
- Throws:
org.hibernate.HibernateException
-
hashCode
- Specified by:
hashCode
in interfaceorg.hibernate.usertype.CompositeUserType
- Throws:
org.hibernate.HibernateException
-
deepCopy
- Specified by:
deepCopy
in interfaceorg.hibernate.usertype.CompositeUserType
- Throws:
org.hibernate.HibernateException
-
isMutable
public boolean isMutable()- Specified by:
isMutable
in interfaceorg.hibernate.usertype.CompositeUserType
-