001/*
002 * Copyright (c) 2008, 2009, 2011 Oracle, Inc. All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the
005 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
006 * which accompanies this distribution.  The Eclipse Public License is available
007 * at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
008 * is available at http://www.eclipse.org/org/documents/edl-v10.php.
009 */
010package javax.persistence;
011
012import java.lang.annotation.Retention;
013import java.lang.annotation.Target;
014
015import static java.lang.annotation.ElementType.FIELD;
016import static java.lang.annotation.ElementType.METHOD;
017import static java.lang.annotation.RetentionPolicy.RUNTIME;
018
019/**
020 * Specifies the map key for associations of type {@link java.util.Map java.util.Map} when the map key is
021 * itself the primary key or a persistent field or property of the entity that is the value of the map.
022 * <p>
023 * If a persistent field or property other than the primary key is used as a map key then it is expected to
024 * have a uniqueness constraint associated with it.
025 * <p>
026 * The {@link MapKeyClass} annotation is not used when <code>MapKey</code> is specified and vice versa.
027 * <p>
028 * <pre>
029 *
030 *    Example 1:
031 *
032 *    &#064;Entity
033 *    public class Department {
034 *        ...
035 *        &#064;OneToMany(mappedBy="department")
036 *        &#064;MapKey  // map key is primary key
037 *        public Map&#060;Integer, Employee&#062; getEmployees() {... }
038 *        ...
039 *    }
040 *
041 *    &#064;Entity
042 *    public class Employee {
043 *        ...
044 *        &#064;Id Integer getEmpId() { ... }
045 *        &#064;ManyToOne
046 *        &#064;JoinColumn(name="dept_id")
047 *        public Department getDepartment() { ... }
048 *        ...
049 *    }
050 *
051 *    Example 2:
052 *
053 *    &#064;Entity
054 *        public class Department {
055 *        ...
056 *        &#064;OneToMany(mappedBy="department")
057 *        &#064;MapKey(name="name")
058 *        public Map&#060;String, Employee&#062; getEmployees() {... }
059 *        ...
060 *    }
061 *
062 *    &#064;Entity
063 *        public class Employee {
064 *        &#064;Id public Integer getEmpId() { ... }
065 *        ...
066 *        &#064;ManyToOne
067 *        &#064;JoinColumn(name="dept_id")
068 *        public Department getDepartment() { ... }
069 *        ...
070 *    }
071 * </pre>
072 *
073 * @since Java Persistence 1.0
074 */
075@Target({METHOD, FIELD})
076@Retention(RUNTIME)
077public @interface MapKey {
078
079  /**
080   * (Optional) The name of the persistent field or property of the associated entity that is used as the
081   * map key.
082   * <p>
083   * Default: If the <code>name</code> element is not specified, the primary key of the associated entity is
084   * used as the map key. If the primary key is a composite primary key and is mapped as
085   * <code>IdClass</code>, an instance of the primary key class is used as the key.
086   *
087   * @return The name
088   */
089  String name() default "";
090}