Fields of a class can either have the values set by you the user, or you can set DataNucleus to generate them for you. This is of particular importance with identity fields where you want unique identities. You can use this value generation process with any field in JDO. There are many different "strategies" for generating values, as defined by the JDO specifications, and also some DataNucleus extensions. Some strategies are specific to a particular datastore, and some are generic. You should choose the strategy that best suits your target datastore. The available strategies for JDO are :-
See also :-
With this strategy DataNucleus will choose the most appropriate strategy for the datastore being used. If you also specify the 'sequence' name attribute and the datastore supports sequences then "sequence" strategy would be used. Otherwise it will always choose "increment" strategy.
A sequence is a user-defined database function that generates a sequence of unique numeric ids. The unique identifier value returned from the database is translated to a java type: java.lang.Long. DataNucleus supports sequences for the following datastores:
To configure a class to use either of these generation methods with datastore identity you simply add this to the class' Meta-Data
<class name="myclass" ... >
<datastore-identity strategy="sequence" sequence="yourseq"/>
...
<sequence name="yourseq" datastore-sequence="YOUR_SEQUENCE_NAME"/>
</class>or using annotations @PersistenceCapable @DatastoreIdentity(strategy="sequence", sequence="yourseq"/> @Sequence(name="yourseq", datastore-sequence="YOUR_SEQUENCE_NAME"/> public class MyClass You replace "YOUR_SEQUENCE_NAME" with your sequence name. To configure a class to use either of these generation methods using application identity you would add the following to the class' Meta-Data
<class name="myclass" ... >
<field name="myfield" primary-key="true" value-strategy="sequence" sequence="yourseq"/>
...
<sequence name="yourseq" datastore-sequence="YOUR_SEQUENCE_NAME"/>
</class>or using annotations
@PersistenceCapable
@Sequence(name="yourseq", datastore-sequence="YOUR_SEQUENCE_NAME"/>
public class MyClass
{
@Persistent(valueStrategy="sequence", sequence="yourseq"/>
private long myfield;
...
}If the sequence does not yet exist in the database at the time DataNucleus needs a new unique identifier, a new sequence is created in the database based on the JDO Meta-Data configuration. Additional properties for configuring sequences are set in the JDO Meta-Data, see the available properties below. Unsupported properties by a database are silently ignored by DataNucleus.
This value generator will generate values unique across different JVMs
Auto-increment/identity/serial are primary key columns that are populated when a row is inserted in the table. These use the databases own keywords on table creation and so rely on having the table structure either created by DataNucleus or having the column with the necessary keyword. DataNucleus supports auto-increment/identity/serial keys for many databases including :
This generation strategy should only be used if there is a single "root" table for the inheritance tree. If you have more than 1 root table (e.g using subclass-table inheritance) then you should choose a different generation strategy For a class using datastore identity you need to set the strategy attribute. You can configure the Meta-Data for the class something like this (replacing 'myclass' with your class name) :
<class name="myclass">
<datastore-identity strategy="identity"/>
...
</class>For a class using application identity you need to set the value-strategy attribute on the primary key field. You can configure the Meta-Data for the class something like this (replacing 'myclass' and 'myfield' with your class and field names) :
<class name="myclass" identity-type="application" objectid-class="myprimarykeyclass">
<field name="myfield" primary-key="true" value-strategy="identity"/<
...
</class>Please be aware that if you have an inheritance tree with the base class defined as using "identity" then the column definition for the PK of the base table will be defined as "AUTO_INCREMENT" or "IDENTITY" or "SERIAL" (dependent on the RDBMS) and all subtables will NOT have this identifier added to their PK column definitions. This is because the identities are assigned in the base table (since all objects will have an entry in the base table). Please note that if using optimistic transactions, this strategy will mean that the value is only set when the object is actually persisted (i.e at flush() or commit()) This value generator will generate values unique across different JVMs
This method is database neutral and uses a sequence table that holds an incrementing sequence value. The unique identifier value returned from the database is translated to a java type: java.lang.Long. This strategy will work with any datastore. This method require a sequence table in the database and creates one if doesn't exist. To configure a datastore identity class to use this generation method you simply add this to the classes Meta-Data.
<class name="myclass" ... >
<datastore-identity strategy="increment"/>
...
</class>To configure an application identity class to use this generation method you simply add this to the class' Meta-Data. If your class is in an inheritance tree you should define this for the base class only.
<class name="myclass" ... >
<field name="myfield" primary-key="true" value-strategy="increment"/>
...
</class>>Additional properties for configuring this generator are set in the JDO Meta-Data, see the available properties below. Unsupported properties are silently ignored by DataNucleus.
This value generator will generate values unique across different JVMs (from DataNucleus 1.1.3)
This generator creates identities with 16 characters in string format. The identity contains the IP address of the local machine where DataNucleus is running, as well as other necessary components to provide uniqueness across time. This generator can be used in concurrent applications. It is especially useful in situations where large numbers of transactions within a certain amount of time have to be made, and the additional overhead of synchronizing the concurrent creation of unique identifiers through the database would break performance limits. It doesn't require datastore access to generate the identities and so has performance benefits over some of the other generators. For a class using datastore identity you need to add metadata something like the following
<class name="myclass" ... >
<datastore-identity strategy="uuid-string"/>
...
</class>To configure an application identity class to use this generation method you simply add this to the class' JDO Meta-Data.
<class name="myclass" ... >
<field name="myfield" primary-key="true" value-strategy="uuid-string"/>
...
</class>
This generator creates identities with 32 characters in hexadecimal format. The identity contains the IP address of the local machine where DataNucleus is running, as well as other necessary components to provide uniqueness across time. This generator can be used in concurrent applications. It is especially useful in situations where large numbers of transactions within a certain amount of time have to be made, and the additional overhead of synchronizing the concurrent creation of unique identifiers through the database would break performance limits. It doesn't require datastore access to generate the identities and so has performance benefits over some of the other generators. For a class using datastore identity you need to add metadata something like the following
<class name="myclass" ... >
<datastore-identity strategy="uuid-hex"/>
...
</class>To configure an application identity class to use this generation method you simply add this to the class' JDO Meta-Data.
<class name="myclass" ... >
<field name="myfield" primary-key="true" value-strategy="uuid-hex"/>
...
</class>
This method is like the "uuid-hex" option above except that it utilises datastore capabilities to generate the UUIDHEX code. Consequently this only works on some RDBMS (MSSQL, MySQL). The disadvantage of this strategy is that it makes a call to the datastore for each new UUID required. The generated UUID is in the same form as the AUID strategy where identities are generated in memory and so the AUID strategy is the recommended choice relative to this option. For a class using datastore identity you need to add metadata something like the following
<class name="myclass" ... >
<datastore-identity strategy="datastore-uuid-hex"/>
...
</class>To configure an application identity class to use this generation method you simply add this to the class' JDO Meta-Data.
<class name="myclass" ... >
<field name="myfield" primary-key="true" value-strategy="datastore-uuid-hex"/>
...
</class>
This method is database neutral and uses the "select max(column) from table" + 1 strategy to create unique ids. The unique identifier value returned from the database is translated to a java type: java.lang.Long. It is however not recommended by DataNucleus since it makes a DB call for every record to be inserted and hence is inefficient. Each DB call will run a scan in all table contents causing contention and locks in the table. We recommend the use of either Sequence or Identity based value generators (see below) - which you use would depend on your RDBMS. For a class using datastore identity you need to add metadata something like the following
<class name="myclass" ... >
<datastore-identity strategy="max"/>
...
</class>To configure an application identity class to use this generation method you simply add this to the class' JDO Meta-Data.
<class name="myclass" ... >
<field name="myfield" primary-key="true" value-strategy="max"/>
...
</class>This value generator will NOT guarantee to generate values unique across different JVMs. This is because it will select the "max+1" and before creating the record another thread may come in and insert one.
This generator uses the JDK1.5 UUID class to generate values. The values are 128-bit (36 character) of the form "0e400c2c-b3a0-4786-a0c6-f2607bf643eb" This generator can be used in concurrent applications. It is especially useful in situations where large numbers of transactions within a certain amount of time have to be made, and the additional overhead of synchronizing the concurrent creation of unique identifiers through the database would break performance limits. For a class using datastore identity you need to add metadata something like the following
<class name="myclass" ... >
<datastore-identity strategy="uuid"/>
...
</class>To configure an application identity class to use this generation method you simply add this to the class' JDO Meta-Data.
<class name="myclass" ... >
<field name="myfield" primary-key="true" value-strategy="uuid"/>
...
</class>
Or using annotations
public class MyClass
{
@Persistent(customValueStrategy="uuid")
String myField;
}This value generator will generate values unique across different JVMs
This generator uses a Java implementation of DCE UUIDs to create unique identifiers without the overhead of additional database transactions or even an open database connection. The identifiers are Strings of the form "LLLLLLLL-MMMM-HHHH-CCCC-NNNNNNNNNNNN" where 'L', 'M', 'H', 'C' and 'N' are the DCE UUID fields named time low, time mid, time high, clock sequence and node. This generator can be used in concurrent applications. It is especially useful in situations where large numbers of transactions within a certain amount of time have to be made, and the additional overhead of synchronizing the concurrent creation of unique identifiers through the database would break performance limits. For a class using datastore identity you need to add metadata something like the following
<class name="myclass" ... >
<datastore-identity strategy="auid"/>
...
</class>To configure an application identity class to use this generation method you simply add this to the class' JDO Meta-Data.
<class name="myclass" ... >
<field name="myfield" primary-key="true" value-strategy="auid"/>
...
</class>This value generator will generate values unique across different JVMs
This method will create a java.sql.Timestamp of the current time (at insertion in the datastore). For a class using datastore identity you need to add metadata something like the following
<class name="myclass" ... >
<datastore-identity strategy="timestamp"/>
...
</class>To configure an application identity class to use this generation method you simply add this to the class' JDO Meta-Data.
<class name="myclass" ... >
<field name="myfield" primary-key="true" value-strategy="timestamp"/>
...
</class>
This method will create a long of the current time in millisecs (at insertion in the datastore). For a class using datastore identity you need to add metadata something like the following
<class name="myclass" ... >
<datastore-identity strategy="timestamp-value"/>
...
</class>To configure an application identity class to use this generation method you simply add this to the class' JDO Meta-Data.
<class name="myclass" ... >
<field name="myfield" primary-key="true" value-strategy="timestamp-value"/>
...
</class>
This section describes how to use the DataNucleus Value Generator API for generating unique keys for objects outside the DataNucleus (JDO) runtime. DataNucleus defines a framework for identity generation and provides many builtin strategies for identities. You can make use of the same strategies described above but for generating identities manually for your own use. The process is described below The DataNucleus Value Generator API revolves around 2 classes. The entry point for retrieving generators is the ValueGenerationManager . This manages the appropriate ValueGenerator classes. Value generators maintain a block of cached ids in memory which avoid reading the database each time it needs a new unique id. Caching a block of unique ids provides you the best performance but can cause "holes" in the sequence of ids for the stored objects in the database. Let's take an example. Here we want to obtain an identity using the TableGenerator ("increment" above). This stores identities in a datastore table. We want to generate an identity using this. Here is what we add to our code
PersistenceManagerImpl pm = (PersistenceManagerImpl) ... // cast your pm to impl ;
// Obtain a ValueGenerationManager
ValueGenerationManager mgr = new ValueGenerationManager();
// Obtain a ValueGenerator of the required type
Properties properties = new Properties();
properties.setProperty("sequence-name", "GLOBAL"); // Use a global sequence number (for all tables)
ValueGenerator generator = mgr.createValueGenerator("MyGenerator",
org.datanucleus.store.rdbms.valuegenerator.TableGenerator.class, props, pm.getStoreManager(),
new ValueGenerationConnectionProvider()
{
RDBMSManager rdbmsManager = null;
ManagedConnection con;
public ManagedConnection retrieveConnection()
{
rdbmsManager = (RDBMSManager) pm.getStoreManager();
try
{
// important to use TRANSACTION_NONE like DataNucleus does
con = rdbmsManager.getConnection(Connection.TRANSACTION_NONE);;
return con;
}
catch (SQLException e)
{
logger.error("Failed to obtain new DB connection for identity generation!");
throw new RuntimeException(e);
}
}
public void releaseConnection()
{
try
{
con.close();
con = null;
}
catch (DataNucleusException e)
{
logger.error("Failed to close DB connection for identity generation!");
throw new RuntimeException(e);
}
finally
{
rdbmsManager = null;
}
}
});
// Retrieve the next identity using this strategy
Long identifier = (Long)generator.next();Some ValueGenerators are specific to RDBMS datastores, and some are generic, so bear this in mind when selecting and adding your own. |