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.RetentionPolicy.RUNTIME;
017
018/**
019 * Specifies the map key for associations of type {@link java.util.Map java.util.Map} when the map key is
020 * itself the primary key or a persistent field or property of the entity that is the value of the map.
021 * <p>
022 * If a persistent field or property other than the primary key is used as a map key then it is expected to
023 * have a uniqueness constraint associated with it.
024 * <p>
025 * The {@link MapKeyClass} annotation is not used when <code>MapKey</code> is specified and vice versa.
026 * <p>
027 * <pre>
028 *
029 *    Example 1:
030 *
031 *    &#064;Entity
032 *    public class Department {
033 *        ...
034 *        &#064;OneToMany(mappedBy="department")
035 *        &#064;MapKey  // map key is primary key
036 *        public Map&#060;Integer, Employee&#062; getEmployees() {... }
037 *        ...
038 *    }
039 *
040 *    &#064;Entity
041 *    public class Employee {
042 *        ...
043 *        &#064;Id Integer getEmpId() { ... }
044 *        &#064;ManyToOne
045 *        &#064;JoinColumn(name="dept_id")
046 *        public Department getDepartment() { ... }
047 *        ...
048 *    }
049 *
050 *    Example 2:
051 *
052 *    &#064;Entity
053 *        public class Department {
054 *        ...
055 *        &#064;OneToMany(mappedBy="department")
056 *        &#064;MapKey(name="name")
057 *        public Map&#060;String, Employee&#062; getEmployees() {... }
058 *        ...
059 *    }
060 *
061 *    &#064;Entity
062 *        public class Employee {
063 *        &#064;Id public Integer getEmpId() { ... }
064 *        ...
065 *        &#064;ManyToOne
066 *        &#064;JoinColumn(name="dept_id")
067 *        public Department getDepartment() { ... }
068 *        ...
069 *    }
070 * </pre>
071 *
072 * @since Java Persistence 1.0
073 */
074@Target({FIELD})
075@Retention(RUNTIME)
076public @interface MapKey {
077
078  /**
079   * (Optional) The name of the persistent field or property of the associated entity that is used as the
080   * map key.
081   * <p>
082   * Default: If the <code>name</code> element is not specified, the primary key of the associated entity is
083   * used as the map key. If the primary key is a composite primary key and is mapped as
084   * <code>IdClass</code>, an instance of the primary key class is used as the key.
085   *
086   * @return The name
087   */
088  String name() default "";
089}