Annotation Interface MapKeyJoinColumn


@Repeatable(MapKeyJoinColumns.class) @Target({METHOD,FIELD}) @Retention(RUNTIME) public @interface MapKeyJoinColumn
Specifies a mapping to an entity that is a map key. The map key join column is in the collection table, join table, or table of the target entity that is used to represent the map. If no MapKeyJoinColumn annotation is specified, a single join column is assumed and the default values apply.

Example 1:

@Entity
public class Company {
    @Id
    int id;
    ...
    @OneToMany   // unidirectional
    @JoinTable(name = "COMPANY_ORGANIZATION",
               joinColumns = @JoinColumn(name = "COMPANY"),
               inverseJoinColumns = @JoinColumn(name = "VICEPRESIDENT"))
    @MapKeyJoinColumn(name = "DIVISION")
    Map<Division, VicePresident> organization;
}

Example 2:

@Entity
public class VideoStore {
    @Id
    int id;
    String name;
    Address location;
    ...
    @ElementCollection
    @CollectionTable(name = "INVENTORY",
                     joinColumns = @JoinColumn(name = "STORE"))
    @Column(name = "COPIES_IN_STOCK")
    @MapKeyJoinColumn(name = "MOVIE", referencedColumnName = "ID")
    Map<Movie, Integer> videoInventory;
    ...
}

@Entity
public class Movie {
    @Id long id;
    String title;
    ...
}

Example 3:

@Entity
public class Student {
    @Id
    int studentId;
    ...
    @ManyToMany  // students and courses are also many-many
    @JoinTable(name = "ENROLLMENTS",
               joinColumns = @JoinColumn(name = "STUDENT"),
               inverseJoinColumns = @JoinColumn(name = "SEMESTER"))
    @MapKeyJoinColumn(name = "COURSE")
    Map<Course, Semester> enrollment;
    ...
}
Since:
2.0
See Also: