add MonetaryAmount example to javadoc
This commit is contained in:
parent
1f755e0598
commit
8aa1647aa2
|
@ -31,6 +31,9 @@ import org.hibernate.metamodel.spi.ValueAccess;
|
|||
* regular embeddable class} with the same logical structure as the
|
||||
* {@linkplain #returnedClass() value type managed by the custom type}.
|
||||
* <p>
|
||||
* Properties of this embeddable class are sorted alphabetically by
|
||||
* name, and assigned an index based on this ordering.
|
||||
* <p>
|
||||
* For example, if we were to implement a {@code CompositeUserType}
|
||||
* for a {@code MonetaryAmount} class, we would also provide a
|
||||
* {@code MonetaryAmountEmbeddable} class with a field for each
|
||||
|
@ -39,8 +42,91 @@ import org.hibernate.metamodel.spi.ValueAccess;
|
|||
* and is never referenced in any entity class. It is a source of
|
||||
* metadata only.
|
||||
* <p>
|
||||
* Properties of this embeddable class are sorted alphabetically by
|
||||
* name, and assigned an index based on this ordering.
|
||||
* Here's a full implementation of {@code CompositeUserType} for an
|
||||
* immutable {@code MonetaryAmount} class:
|
||||
* <pre>
|
||||
* public class MonetaryAmountUserType implements CompositeUserType<MonetaryAmount> {
|
||||
*
|
||||
* @Override
|
||||
* public Object getPropertyValue(MonetaryAmount component, int property) {
|
||||
* switch ( property ) {
|
||||
* case 0:
|
||||
* return component.getCurrency();
|
||||
* case 1:
|
||||
* return component.getValue();
|
||||
* }
|
||||
* throw new HibernateException( "Illegal property index: " + property );
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public MonetaryAmount instantiate(ValueAccess valueAccess, SessionFactoryImplementor sessionFactory) {
|
||||
* final BigDecimal value = valueAccess.getValue(0, BigDecimal.class);
|
||||
* final Currency currency = valueAccess.getValue(1, Currency.class);
|
||||
*
|
||||
* if ( value == null && currency == null ) {
|
||||
* return null;
|
||||
* }
|
||||
* return new MonetaryAmount( value, currency );
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public Class embeddable() {
|
||||
* return MonetaryAmountEmbeddable.class;
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public Class<MonetaryAmount> returnedClass() {
|
||||
* return MonetaryAmount.class;
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public boolean isMutable() {
|
||||
* return false;
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public MonetaryAmount deepCopy(MonetaryAmount value) {
|
||||
* return value; // MonetaryAmount is immutable
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public boolean equals(MonetaryAmount x, MonetaryAmount y) {
|
||||
* if ( x == y ) {
|
||||
* return true;
|
||||
* }
|
||||
* if ( x == null || y == null ) {
|
||||
* return false;
|
||||
* }
|
||||
* return x.equals( y );
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public Serializable disassemble(MonetaryAmount value) {
|
||||
* return value;
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public MonetaryAmount assemble(Serializable cached, Object owner) {
|
||||
* return (MonetaryAmount) cached;
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public MonetaryAmount replace(MonetaryAmount original, MonetaryAmount target, Object owner) {
|
||||
* return original;
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public int hashCode(MonetaryAmount x) throws HibernateException {
|
||||
* return x.hashCode();
|
||||
* }
|
||||
*
|
||||
* // the embeddable class which acts as a source of metadata
|
||||
* public static class MonetaryAmountEmbeddable {
|
||||
* private BigDecimal value;
|
||||
* private Currency currency;
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
* <p>
|
||||
* Every implementor of {@code CompositeUserType} must be immutable
|
||||
* and must declare a public default constructor.
|
||||
|
|
Loading…
Reference in New Issue