Skip to content

Package: EmbeddedId

EmbeddedId

Coverage

1: /*
2: * Copyright (c) 2008, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0 which is available at
6: * http://www.eclipse.org/legal/epl-2.0,
7: * or the Eclipse Distribution License v. 1.0 which is available at
8: * http://www.eclipse.org/org/documents/edl-v10.php.
9: *
10: * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11: */
12:
13: // Contributors:
14: // Linda DeMichiel - 2.1
15: // Linda DeMichiel - 2.0
16:
17: package jakarta.persistence;
18:
19: import java.lang.annotation.Target;
20: import java.lang.annotation.Retention;
21: import static java.lang.annotation.ElementType.FIELD;
22: import static java.lang.annotation.ElementType.METHOD;
23: import static java.lang.annotation.RetentionPolicy.RUNTIME;
24:
25: /**
26: * Specifies that the annotated persistent field or property
27: * of an entity class or mapped superclass is the composite
28: * primary key of the entity. The type of the annotated field
29: * or property must be an {@linkplain Embeddable embeddable}
30: * type, and must be explicitly annotated {@link Embeddable}.
31: *
32: * <p>If a field or property of an entity class is annotated
33: * {@code EmbeddedId}, then no other field or property of the
34: * entity may be annotated {@link Id} or {@code EmbeddedId},
35: * and the entity class must not declare an {@link IdClass}.
36: *
37: * <p>The embedded primary key type must implement
38: * {@link #equals} and {@link #hashCode}, defining value
39: * equality consistently with equality of the mapped primary
40: * key of the database table.
41: *
42: * <p>The {@link AttributeOverride} annotation may be used to
43: * override the column mappings declared within the embeddable
44: * class.
45: *
46: * <p>The {@link MapsId} annotation may be used in conjunction
47: * with the {@code EmbeddedId} annotation to declare a derived
48: * primary key.
49: *
50: * <p>If the entity has a derived primary key, the
51: * {@link AttributeOverride} annotation may only be used to
52: * override those attributes of the embedded id that do not
53: * correspond to the relationship to the parent entity.
54: *
55: * <p>Relationship mappings defined within an embedded primary
56: * key type are not supported.
57: *
58: * <p>Example 1:
59: * {@snippet :
60: * @Entity
61: * public class Employee {
62: * @EmbeddedId
63: * protected EmployeePK empPK;
64: * ...
65: * }
66: *
67: * public record EmployeePK(String empName, Date birthDay) {}
68: * }
69: *
70: * <p>Example 2:
71: * {@snippet :
72: * @Embeddable
73: * public class DependentId {
74: * String name;
75: * EmployeeId empPK; // corresponds to primary key type of Employee
76: * }
77: *
78: * @Entity
79: * public class Dependent {
80: * // default column name for "name" attribute is overridden
81: * @AttributeOverride(name = "name", column = @Column(name = "dep_name"))
82: * @EmbeddedId
83: * DependentId id;
84: * ...
85: * @MapsId("empPK")
86: * @ManyToOne
87: * Employee emp;
88: * }
89: * }
90: *
91: * @see Embeddable
92: * @see MapsId
93: * @see IdClass
94: *
95: * @since 1.0
96: */
97: @Target({METHOD, FIELD})
98: @Retention(RUNTIME)
99: public @interface EmbeddedId {}