-
@Target(value={METHOD,FIELD}) @Retention(value=RUNTIME) public @interface MapKey
Specifies the map key for associations of typejava.util.Map
when the map key is itself the primary key or a persistent field or property of the entity that is the value of the map.If a persistent field or property other than the primary key is used as a map key then it is expected to have a uniqueness constraint associated with it.
The
MapKeyClass
annotation is not used whenMapKey
is specified and vice versa.Example 1: @Entity public class Department { ... @OneToMany(mappedBy="department") @MapKey // map key is primary key public Map<Integer, Employee> getEmployees() {... } ... } @Entity public class Employee { ... @Id Integer getEmpId() { ... } @ManyToOne @JoinColumn(name="dept_id") public Department getDepartment() { ... } ... } Example 2: @Entity public class Department { ... @OneToMany(mappedBy="department") @MapKey(name="name") public Map<String, Employee> getEmployees() {... } ... } @Entity public class Employee { @Id public Integer getEmpId() { ... } ... @ManyToOne @JoinColumn(name="dept_id") public Department getDepartment() { ... } ... }
- Since:
- Java Persistence 1.0
-
-
Element Detail
-
name
public abstract String name
(Optional) The name of the persistent field or property of the associated entity that is used as the map key.Default: If the
name
element is not specified, the primary key of the associated entity is used as the map key. If the primary key is a composite primary key and is mapped asIdClass
, an instance of the primary key class is used as the key.- Default:
- ""
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
21/08/2025 18:30:19 Cette version de la page est en cache (à la date du 21/08/2025 18:30:19) afin d'accélérer le traitement.Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la version plus récente de la page.
Document créé le 19/04/2008, dernière modification le 18/08/2025
Source du document imprimé : https://www.gaudry.be/java-api-javaee-rf-javax/persistence/MapKey.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.