Debian Codegraph @

lang/java/src/com/sleepycat/persist/model/SecondaryKey.java

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 */
7
8 package com.sleepycat.persist.model;
9
10 import static java.lang.annotation.ElementType.FIELD;
11 import static java.lang.annotation.RetentionPolicy.RUNTIME;
12
13 import java.lang.annotation.Documented;
14 import java.lang.annotation.Retention;
15 import java.lang.annotation.Target;
16
17 import com.sleepycat.persist.PrimaryIndex;
18 import com.sleepycat.persist.StoreConfig;
19
20 /**
21 * Indicates a secondary key field of an entity class. The value of the
22 * secondary key field is a unique or non-unique identifier for the entity and
23 * is accessed via a {@link com.sleepycat.persist.SecondaryIndex}.
24 *
25 * <p>{@code SecondaryKey} may appear on any number of fields in an entity
26 * class, subclasses and superclasses. For a secondary key field in the entity
27 * class or one of its superclasses, all entity instances will be indexed by
28 * that field (if it is non-null). For a secondary key field in an entity
29 * subclass, only instances of that subclass will be indexed by that field (if
30 * it is non-null).</p>
31 *
32 * <p>If a secondary key field is null, the entity will not be indexed by that
33 * key. In other words, the entity cannot be queried by that secondary key nor
34 * can the entity be found by iterating through the secondary index.</p>
35 *
36 * <p>For a given entity class and its superclasses and subclasses, no two
37 * secondary keys may have the same name. By default, the field name
38 * identifies the secondary key and the secondary index for a given entity
39 * class. {@link #name} may be specified to override this default.</p>
40 *
41 * <p>Using {@link #relate}, instances of the entity class are related to
42 * secondary keys in a many-to-one, one-to-many, many-to-many, or one-to-one
43 * relationship. This required property specifies the <em>cardinality</em> of
44 * each side of the relationship.</p>
45 *
46 * <p>A secondary key may optionally be used to form a relationship with
47 * instances of another entity class using {@link #relatedEntity} and {@link
48 * #onRelatedEntityDelete}. This establishes <em>foreign key constraints</em>
49 * for the secondary key.</p>
50 *
51 * <p>The secondary key field type must be a Set, Collection or array type when
52 * a <em>x-to-many</em> relationship is used or a singular type when an
53 * <em>x-to-one</em> relationship is used; see {@link #relate}.</p>
54 *
55 * <p>The field type (or element type, when a Set, Collection or array type is
56 * used) of a secondary key field must follow the same rules as for a {@link
57 * <a href="PrimaryKey.html#keyTypes">primary key type</a>}. The {@link <a
58 * href="PrimaryKey.html#sortOrder">key sort order</a>} is also the same.</p>
59 *
60 * <p>For a secondary key field with a collection type, a type parameter must
61 * be used to specify the element type. For example {@code Collection<String>}
62 * is allowed but {@code Collection} is not.</p>
63 *
64 * @author Mark Hayes
65 */
66 @Documented @Retention(RUNTIME) @Target(FIELD)
67 public @interface SecondaryKey {
68
69 /**
70 * Defines the relationship between instances of the entity class and the
71 * secondary keys.
72 *
73 * <p>The table below summarizes how to create all four variations of
74 * relationships.</p>
75 * <div>
76 * <table border="yes">
77 * <tr><th>Relationship</th>
78 * <th>Field type</th>
79 * <th>Key type</th>
80 * <th>Example</th>
81 * </tr>
82 * <tr><td>{@link Relationship#ONE_TO_ONE}</td>
83 * <td>Singular</td>
84 * <td>Unique</td>
85 * <td>A person record with a unique social security number
86 * key.</td>
87 * </tr>
88 * <tr><td>{@link Relationship#MANY_TO_ONE}</td>
89 * <td>Singular</td>
90 * <td>Duplicates</td>
91 * <td>A person record with a non-unique employer key.</td>
92 * </tr>
93 * <tr><td>{@link Relationship#ONE_TO_MANY}</td>
94 * <td>Set/Collection/array</td>
95 * <td>Unique</td>
96 * <td>A person record with multiple unique email address keys.</td>
97 * </tr>
98 * <tr><td>{@link Relationship#MANY_TO_MANY}</td>
99 * <td>Set/Collection/array</td>
100 * <td>Duplicates</td>
101 * <td>A person record with multiple non-unique organization
102 * keys.</td>
103 * </tr>
104 * </table>
105 * </div>
106 *
107 * <p>For a <em>many-to-x</em> relationship, the secondary index will
108 * have non-unique keys; in other words, duplicates will be allowed.
109 * Conversely, for <em>one-to-x</em> relationship, the secondary index
110 * will have unique keys.</p>
111 *
112 * <p>For a <em>x-to-one</em> relationship, the secondary key field is
113 * singular; in other words, it may not be a Set, Collection or array type.
114 * Conversely, for a <em>x-to-many</em> relationship, the secondary key
115 * field must be a Set, Collection or array type. A collection type is any
116 * implementation of {@link java.util.Collection}.</p>
117 *
118 * <p>For a <em>x-to-many</em> relationship, the field type should normally
119 * be {@link java.util.Set} (or a subtype of this interface). This
120 * accurately expresses the fact that an Entity may not have two identical
121 * secondary keys. For flexibility, a {@link java.util.Collection} (or a
122 * subtype of this interface) or an array type may also be used. In that
123 * case, any duplicate key values in the Collection or array are
124 * ignored.</p>
125 */
126 Relationship relate();
127
128 /**
129 * Specifies the entity to which this entity is related, for establishing
130 * foreign key constraints. Values of this secondary key will be
131 * constrained to the set of primary key values for the given entity class.
132 *
133 * <p>The given class must be an entity class. This class is called the
134 * <em>related entity</em> or <em>foreign entity</em>.</p>
135 *
136 * <p>When a related entity class is specified, a check (foreign key
137 * constraint) is made every time a new secondary key value is stored for
138 * this entity, and every time a related entity is deleted.</p>
139 *
140 * <p>Whenever a new secondary key value is stored for this entity, it is
141 * checked to ensure it exists as a primary key value of the related
142 * entity. If it does not, an exception is thrown by the {@link
143 * PrimaryIndex} {@code put} method.
144 * </p>
145 *
146 * <p>Whenever a related entity is deleted and its primary key value exists
147 * as a secondary key value for this entity, the action is taken that is
148 * specified using the {@link #onRelatedEntityDelete} property.</p>
149 *
150 * <p>Together, these two checks guarantee that a secondary key value for
151 * this entity will always exist as a primary key value for the related
152 * entity. Note, however, that a transactional store must be configured
153 * to guarantee this to be true in the face of a crash; see {@link
154 * StoreConfig#setTransactional}.</p>
155 */
156 Class relatedEntity() default void.class;
157
158 /**
159 * Specifies the action to take when a related entity is deleted having a
160 * primary key value that exists as a secondary key value for this entity.
161 *
162 * <p><em>Note:</em> This property only applies when {@link #relatedEntity}
163 * is specified to define the related entity.</p>
164 *
165 * <p>The default action, {@link DeleteAction#ABORT ABORT}, means that an
166 * exception is thrown in order to abort the current transaction.
167 * </p>
168 *
169 * <p>If {@link DeleteAction#CASCADE CASCADE} is specified, then this
170 * entity will be deleted also. This in turn could trigger further
171 * deletions, causing a cascading effect.</p>
172 *
173 * <p>If {@link DeleteAction#NULLIFY NULLIFY} is specified, then the
174 * secondary key in this entity is set to null and this entity is updated.
175 * If the key field type is singular, the field value is set to null;
176 * therefore, to specify {@code NULLIFY} for a singular key field type, a
177 * primitive wrapper type must be used instead of a primitive type. If the
178 * key field type is an array or collection type, the key is deleted from
179 * the array (the array is resized) or from the collection (using {@link
180 * java.util.Collection#remove Collection.remove}).</p>
181 */
182 DeleteAction onRelatedEntityDelete() default DeleteAction.ABORT;
183
184 /**
185 * Specifies the name of the key in order to use a name that is different
186 * than the field name.
187 *
188 * <p>This is convenient when prefixes or suffices are used on field names.
189 * For example:</p>
190 * <pre class="code">
191 * class Person {
192 * {@literal @SecondaryKey(relate=MANY_TO_ONE, relatedEntity=Person.class, name="parentSsn")}
193 * String m_parentSsn;
194 * }</pre>
195 *
196 * <p>It can also be used to uniquely name a key when multiple secondary
197 * keys for a single entity class have the same field name. For example,
198 * an entity class and its subclass may both have a field named 'date',
199 * and both fields are used as secondary keys. The {@code name} property
200 * can be specified for one or both fields to give each key a unique
201 * name.</p>
202 */
203 String name() default "";
204 }