{@code EntityStore} objects are thread-safe. Multiple threads may safelycall the methods of a shared {@code EntityStore} object.
See the {@link package summary example} for an example of using an {@code EntityStore}.
Before creating an EntityStore
you must create an {@link Environment} object using the Berkeley DB engine API. The environment maycontain any number of entity stores and their associated databases, as well as other databases not associated with an entity store.
An entity store is based on an {@link EntityModel}: a data model which defines persistent classes (entity classes), primary keys, secondary keys, and relationships between entities. A primary index is created for each entity class. An associated secondary index is created for each secondary key. The {@link Entity}, {@link PrimaryKey} and {@link SecondaryKey} annotations may be used to define entities and keys.
To use an EntityStore
, first obtain {@link PrimaryIndex} and{@link SecondaryIndex} objects by calling {@link #getPrimaryIndex getPrimaryIndex} and {@link #getSecondaryIndex getSecondaryIndex}. Then use these indices to store and access entity records by key.
Although not normally needed, you can also use the entity store along with the {@link com.sleepycat.je Base API}. Methods in the {@link PrimaryIndex} and {@link SecondaryIndex} classes may be used to obtaindatabases and bindings. The databases may be used directly for accessing entity records. The bindings should be called explicitly to translate between {@link com.sleepycat.je.DatabaseEntry} objects and entity modelobjects.
Each primary and secondary index is associated internally with a {@link Database}. With any of the above mentioned use cases, methods are provided that may be used for database performance tuning. The {@link #setPrimaryConfig setPrimaryConfig} and {@link #setSecondaryConfig setSecondaryConfig} methods may be called anytime before a database isopened via {@link #getPrimaryIndex getPrimaryIndex} or {@link #getSecondaryIndex getSecondaryIndex}. The {@link #setSequenceConfig setSequenceConfig} method may be called anytime before {@link #getSequence getSequence} is called or {@link #getPrimaryIndex getPrimaryIndex} is calledfor a primary index associated with that sequence.
The database names of primary and secondary indices are designed to be unique within the environment and identifiable for debugging and use with tools such as {@link com.sleepycat.je.util.DbDump} and {@link com.sleepycat.je.util.DbLoad}.
The syntax of a primary index database name is:
persist#STORE_NAME#ENTITY_CLASS
Where STORE_NAME is the name parameter passed to {@link #EntityStore EntityStore} and ENTITY_CLASS is name of the class passed to {@link #getPrimaryIndex getPrimaryIndex}.
The syntax of a secondary index database name is:
persist#STORE_NAME#ENTITY_CLASS#KEY_NAME
Where KEY_NAME is the secondary key name passed to {@link #getSecondaryIndex getSecondaryIndex}.
Although you should never have to construct these names manually, understanding their syntax is useful for several reasons:
EntityStore
, to avoid naming conflicts the other database names should not begin with "persist#"
."persist#STORE_NAME#"
.If you are copying all databases in a store as mentioned in the last point above, there is one further consideration. There are two internal databases that must be kept with the other databases in the store in order for the store to be used. These contain the data formats and sequences for the store:
persist#STORE_NAME#com.sleepycat.persist.formats
persist#STORE_NAME#com.sleepycat.persist.sequences
These databases must normally be included with copies of other databases in the store. They should not be modified by the application.
For example, the following code snippet removes all databases for a given store in a single transaction.
Environment env = ... EntityStore store = ... Transaction txn = env.beginTransaction(null, null); String prefix = "persist#" + store.getStoreName() + "#"; for (String dbName : env.getDatabaseNames()) { if (dbName.startsWith(prefix)) { env.removeDatabase(txn, dbName); } } txn.commit();@author Mark Hayes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|