Updating an entity's identifier within your model

You can download the code for this example here:

OnyxDevTools/onyx-database-samples/model-updates/ ... UpdateIdentifierDemo.java.

There are 2 parts to this example. The first part is to create an existing database containing the original structure of your model. The second is to modify specific parts of the model and observe how the database handles the update.

This particular change involves changing the data type for an identifier and removing the generator from the @Identifier annotation. That indicates you would no longer like to automatically generate unique identifiers.

  1. Define a model including the Account entity.

    When defining an entity, begin with the end in mind. You should define the auto generated identifier as a long in order to account for a larger record set. In this case, we choose to ignore that reason and declare the identifier as an integer to demonstrate how Onyx has a remedy for that mistake.

  2. Create a script to seed some test data

    In order to observe how the database reacts to changes we need to create a script to load sample data before updating the model of the Account so that we may preserve the format of the stored Accounts.

    Notes:
    Prior to running this script, ensure the database is deleted so that we can start with a clean slate.
    If the script runs successfully you have created an Account
  3. Run the Main class

    Connect to a new database and seed the data.

    Notes:
    Take note of the database location as you will need it later to use in another script.
  4. Modify the Account's Identifier

    Change the account to a Long and remove the identifier generator.

    Notes:
    This is an example of how the identifier can be changed to handle a larger precision. If you choose to change the attribute type from a long to an int, you must ensure the largest stored value is smaller than the maximum value of an integer.
  5. Re-Connect to the Database

    Create a script to reconnect to the database.

    Notes:
    The database location should be the same as what was declared in the first script.
  6. Verify New Fields

    Retrieve the account using a long rather than int.

    Notes:
    Removing the generator is easily supported. The identifier value will not be auto generated any more but, if you were to add a generator you must reset the last generated identifier by saving an entity with the latest sequence value. Onyx will not automatically determine the first sequence value and will start at 1.
  7. Relationship Updates