Annotation Interface MapKeyEnumerated


@Target({METHOD,FIELD}) @Retention(RUNTIME) public @interface MapKeyEnumerated
Specifies that a map key should be persisted as an enumerated type. The declared type of the annotated property or field must be Map<K,V> where K is a Java enum type. This annotation is always optional.

The MapKeyEnumerated annotation may be used in conjunction with the ElementCollection annotation, OneToMany annotation, or ManyToMany annotation when the key type of the collection is an enum type.

An enum can be mapped as either a string or an integer, where EnumType enumerates the available options. The mapping may be explicitly specified by this annotation.

If a map key of enum type has no explicit MapKeyEnumerated annotation, and if no converter is applied to the key:

Example:

public enum ProjectStatus {COMPLETE, DELAYED, CANCELLED, IN_PROGRESS}

public enum SalaryRate {JUNIOR, SENIOR, MANAGER, EXECUTIVE}

@Entity
public class Employee {
    @ManyToMany
    public Projects<ProjectStatus, Project> getProjects() { ... }

    @OneToMany
    @MapKeyEnumerated(STRING)
    public Map<SalaryRate, Employee> getEmployees() { ... }
    ...
}
Since:
2.0
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    (Optional) The type used in mapping a map key enum type.
  • Element Details

    • value

      EnumType value
      (Optional) The type used in mapping a map key enum type.
      Default:
      ORDINAL