1 /* 2 * The contents of this file are subject to the terms 3 * of the Common Development and Distribution License 4 * (the License). You may not use this file except in 5 * compliance with the License. 6 * 7 * You can obtain a copy of the license at 8 * https://glassfish.dev.java.net/public/CDDLv1.0.html or 9 * glassfish/bootstrap/legal/CDDLv1.0.txt. 10 * See the License for the specific language governing 11 * permissions and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL 14 * Header Notice in each file and include the License file 15 * at glassfish/bootstrap/legal/CDDLv1.0.txt. 16 * If applicable, add the following below the CDDL Header, 17 * with the fields enclosed by brackets [] replaced by 18 * you own identifying information: 19 * "Portions Copyrighted [year] [name of copyright owner]" 20 * 21 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 22 */ 23 package javax.persistence; 24 25 import java.lang.annotation.Target; 26 import java.lang.annotation.Retention; 27 import static java.lang.annotation.ElementType.FIELD; 28 import static java.lang.annotation.ElementType.METHOD; 29 import static java.lang.annotation.RetentionPolicy.RUNTIME; 30 31 /** 32 * Is used to specify the map key for associations of type 33 * {@link java.util.Map}. 34 * 35 * <p> If a persistent field or property other than the primary 36 * key is used as a map key then it is expected to have a 37 * uniqueness constraint associated with it. 38 * 39 * <pre> 40 * 41 * Example 1: 42 * 43 * @Entity 44 * public class Department { 45 * ... 46 * @OneToMany(mappedBy="department") 47 * @MapKey(name="empId") 48 * public Map<Integer, Employee> getEmployees() {... } 49 * ... 50 * } 51 * 52 * @Entity 53 * public class Employee { 54 * ... 55 * @Id Integer getEmpid() { ... } 56 * @ManyToOne 57 * @JoinColumn(name="dept_id") 58 * public Department getDepartment() { ... } 59 * ... 60 * } 61 * 62 * Example 2: 63 * 64 * @Entity 65 * public class Department { 66 * ... 67 * @OneToMany(mappedBy="department") 68 * @MapKey(name="empPK") 69 * public Map<EmployeePK, Employee> getEmployees() {... } 70 * ... 71 * } 72 * 73 * @Entity 74 * public class Employee { 75 * @EmbeddedId public EmployeePK getEmpPK() { ... } 76 * ... 77 * @ManyToOne 78 * @JoinColumn(name="dept_id") 79 * public Department getDepartment() { ... } 80 * ... 81 * } 82 * 83 * @Embeddable 84 * public class EmployeePK { 85 * String name; 86 * Date bday; 87 * } 88 * </pre> 89 * 90 * @since Java Persistence 1.0 91 */ 92 @Target({METHOD, FIELD}) 93 @Retention(RUNTIME) 94 95 public @interface MapKey { 96 97 /** 98 * The name of the persistent field or property of the 99 * associated entity that is used as the map key. If the 100 * name element is not specified, the primary key of the 101 * associated entity is used as the map key. If the 102 * primary key is a composite primary key and is mapped 103 * as {@link IdClass}, an instance of the primary key 104 * class is used as the key. 105 */ 106 String name() default ""; 107 } 108