| 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.EntityStore; |
| 18 |
import com.sleepycat.persist.PrimaryIndex; |
| 19 |
|
| 20 |
/** |
| 21 |
* Indicates the primary key field of an entity class. The value of the |
| 22 |
* primary key field is the unique identifier for the entity in a {@link |
| 23 |
* PrimaryIndex}. |
| 24 |
* |
| 25 |
* <p>{@link PrimaryKey} may appear on at most one declared field per |
| 26 |
* class.</p> |
| 27 |
* |
| 28 |
* <p>Primary key values may be automatically assigned as sequential integers |
| 29 |
* using a {@link #sequence}. In this case the type of the key field is |
| 30 |
* restricted to a simple integer type.</p> |
| 31 |
* |
| 32 |
* <p>A primary key field may not be null, unless it is being assigned from a |
| 33 |
* sequence.</p> |
| 34 |
* |
| 35 |
* <p><a name="keyTypes"><strong>Key Field Types</strong></a></p> |
| 36 |
* |
| 37 |
* <p>The type of a key field must either be one of the following:</p> |
| 38 |
* <ul> |
| 39 |
* <li>Any of the {@link <a href="Entity.html#simpleTypes">simple |
| 40 |
* types</a>}.</li> |
| 41 |
* <li>An enum type.</p> |
| 42 |
* <li>A composite key class containing one or more simple type or enum |
| 43 |
* fields.</li> |
| 44 |
* </ul> |
| 45 |
* <p>Array types are not allowed.</p> |
| 46 |
* |
| 47 |
* <p>When using a composite key class, each field of the composite key class |
| 48 |
* must be annotated with {@link KeyField} to identify the storage order and |
| 49 |
* default sort order. See {@link KeyField} for an example and more |
| 50 |
* information on composite keys.</p> |
| 51 |
* |
| 52 |
* <p><a name="sortOrder"><strong>Key Sort Order</strong></a></p> |
| 53 |
* |
| 54 |
* <p>Key field types, being simple types, have a well defined and reasonable |
| 55 |
* default sort order, described below. This sort order is based on a storage |
| 56 |
* encoding that allows a fast byte-by-byte comparison.</p> |
| 57 |
* <ul> |
| 58 |
* <li>All simple types except for {@code String} are encoded so that they are |
| 59 |
* sorted as expected, that is, as if the {@link Comparable#compareTo} method |
| 60 |
* of their class (or, for primitives, their wrapper class) is called.</li> |
| 61 |
* <br> |
| 62 |
* <li>Strings are encoded as UTF-8 byte arrays. Zero (0x0000) character |
| 63 |
* values are UTF encoded as non-zero values, and therefore embedded zeros in |
| 64 |
* the string are supported. The sequence {@literal {0xC0,0x80}} is used to |
| 65 |
* encode a zero character. This UTF encoding is the same one used by native |
| 66 |
* Java UTF libraries. However, this encoding of zero does impact the |
| 67 |
* lexicographical ordering, and zeros will not be sorted first (the natural |
| 68 |
* order) or last. For all character values other than zero, the default UTF |
| 69 |
* byte ordering is the same as the Unicode lexicographical character |
| 70 |
* ordering.</li> |
| 71 |
* </ul> |
| 72 |
* |
| 73 |
* <p>When using a composite key class with more than one field, the sorting |
| 74 |
* order among fields is determined by the {@link KeyField} annotations. To |
| 75 |
* override the default sort order, you can use a composite key class that |
| 76 |
* implements {@link Comparable}. This allows overriding the sort order and is |
| 77 |
* therefore useful even when there is only one key field in the composite key |
| 78 |
* class. See {@link <a href="KeyField.html#comparable">Custom Sort Order</a>} |
| 79 |
* for more information on sorting of composite keys.</p> |
| 80 |
* |
| 81 |
* <p><a name="inherit"><strong>Inherited Primary Key</strong></a></p> |
| 82 |
* |
| 83 |
* <p>If it does not appear on a declared field in the entity class, {@code |
| 84 |
* PrimaryKey} must appear on a field of an entity superclass. In the |
| 85 |
* following example, the primary key on the base class is used:</p> |
| 86 |
* |
| 87 |
* <pre class="code"> |
| 88 |
* {@literal @Persistent} |
| 89 |
* class BaseClass { |
| 90 |
* {@literal @PrimaryKey} |
| 91 |
* long id; |
| 92 |
* ... |
| 93 |
* } |
| 94 |
* {@literal @Entity} |
| 95 |
* class Employee extends BaseClass { |
| 96 |
* // inherits id primary key |
| 97 |
* ... |
| 98 |
* }</pre> |
| 99 |
* |
| 100 |
* <p>If more than one class with {@code PrimaryKey} is present in a class |
| 101 |
* hierarchy, the key in the most derived class is used. In this case, primary |
| 102 |
* key fields in superclasses are "shadowed" and are not persistent. In the |
| 103 |
* following example, the primary key in the base class is not used and is not |
| 104 |
* persistent:</p> |
| 105 |
* <pre class="code"> |
| 106 |
* {@literal @Persistent} |
| 107 |
* class BaseClass { |
| 108 |
* {@literal @PrimaryKey} |
| 109 |
* long id; |
| 110 |
* ... |
| 111 |
* } |
| 112 |
* {@literal @Entity} |
| 113 |
* class Employee extends BaseClass { |
| 114 |
* // overrides id primary key |
| 115 |
* {@literal @PrimaryKey} |
| 116 |
* String uuid; |
| 117 |
* ... |
| 118 |
* }</pre> |
| 119 |
* |
| 120 |
* <p>Note that a {@code PrimaryKey} is not allowed on entity subclasses. The |
| 121 |
* following is illegal and will cause an {@code IllegalArgumentException} when |
| 122 |
* trying to store an {@code Employee} instance:</p> |
| 123 |
* <pre class="code"> |
| 124 |
* {@literal @Entity} |
| 125 |
* class Person { |
| 126 |
* {@literal @PrimaryKey} |
| 127 |
* long id; |
| 128 |
* ... |
| 129 |
* } |
| 130 |
* {@literal @Persistent} |
| 131 |
* class Employee extends Person { |
| 132 |
* {@literal @PrimaryKey} |
| 133 |
* String uuid; |
| 134 |
* ... |
| 135 |
* }</pre> |
| 136 |
* |
| 137 |
* @author Mark Hayes |
| 138 |
*/ |
| 139 |
@Documented @Retention(RUNTIME) @Target(FIELD) |
| 140 |
public @interface PrimaryKey { |
| 141 |
|
| 142 |
/** |
| 143 |
* The name of a sequence from which to assign primary key values |
| 144 |
* automatically. If a non-empty string is specified, sequential integers |
| 145 |
* will be assigned from the named sequence. |
| 146 |
* |
| 147 |
* <p>A single sequence may be used for more than one entity class by |
| 148 |
* specifying the same sequence name for each {@code PrimaryKey}. For |
| 149 |
* each named sequence, a {@link com.sleepycat.db.Sequence} will be used to |
| 150 |
* assign key values. For more information on configuring sequences, see |
| 151 |
* {@link EntityStore#setSequenceConfig EntityStore.setSequenceConfig}.</p> |
| 152 |
* |
| 153 |
* <p>To use a sequence, the type of the key field must be a primitive |
| 154 |
* integer type ({@code byte}, {@code short}, {@code int} or {@code long}) |
| 155 |
* or the primitive wrapper class for one of these types. A composite key |
| 156 |
* class may also be used to override sort order, but it may contain only a |
| 157 |
* single key field, and this field must have one of the types previously |
| 158 |
* mentioned.</p> |
| 159 |
* |
| 160 |
* <p>When an entity with a primary key sequence is stored using one of the |
| 161 |
* <code>put</code> methods in the {@link PrimaryIndex}, a new key will be |
| 162 |
* assigned if the primary key field in the entity instance is null (for a |
| 163 |
* reference type) or zero (for a primitive integer type). Specifying zero |
| 164 |
* for a primitive integer key field is allowed because the initial value |
| 165 |
* of the sequence is one (not zero) by default. If the sequence |
| 166 |
* configuration is changed such that zero is part of the sequence, then |
| 167 |
* the field type must be a primitive wrapper class and the field value |
| 168 |
* must be null to cause a new key to be assigned.</p> |
| 169 |
* |
| 170 |
* <p>When one of the <code>put</code> methods in the {@link PrimaryIndex} |
| 171 |
* is called and a new key is assigned, the assigned value is returned to |
| 172 |
* the caller via the key field of the entity object that is passed as a |
| 173 |
* parameter.</p> |
| 174 |
*/ |
| 175 |
String sequence() default ""; |
| 176 |
} |