Annotation Interface Type


Specifies a custom UserType for the annotated attribute mapping. This annotation may be applied:
  • directly to a property or field of an entity to specify the custom type of the property or field,
  • indirectly, as a meta-annotation of an annotation type that is then applied to various properties and fields, or
  • by default, via a registration.

For example, as an alternative to:

@Type(MonetaryAmountUserType.class)
BigDecimal amount;

we may define a custom annotation type:

@Retention(RUNTIME)
@Target({METHOD,FIELD})
@Type(MonetaryAmountUserType.class)
public @interface MonetaryAmount {}

and then write:

@MonetaryAmount
BigDecimal amount;

which is much cleaner.

An implementation of UserType applied via a custom annotation may declare a constructor which accepts the annotation instance, allowing the annotation to be used to configure the type.

@Retention(RUNTIME)
@Target({METHOD,FIELD})
@Type(MonetaryAmountUserType.class)
public @interface MonetaryAmount {
    public Currency currency();
}
public class MonetaryAmountUserType implements UserType<Amount> {
    private final Currency currency;
    public MonetaryAmountUserType(MonetaryAmount annotation) {
        currency = annotation.currency();
    }
    ...
}

The use of a UserType is usually mutually exclusive with the compositional approach of JavaType and JdbcType.

See Also:
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    Class<? extends UserType<?>>
    The class which implements UserType.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Parameters to be injected into the custom type after it is instantiated.
  • Element Details

    • value

      Class<? extends UserType<?>> value
      The class which implements UserType.
    • parameters

      Parameter[] parameters
      Parameters to be injected into the custom type after it is instantiated. The UserType implementation may implement ParameterizedType to receive the parameters, or it may obtain them via UserTypeCreationContext.getParameters().
      API Note:
      A better approach is to declare a custom annotation type, as described above, and specify parameters in a type safe way as members of the custom annotation.
      Default:
      {}