Type-Specific Identifier Generation

In the previous article, Global Identifier generation conventions were introduced. Any customization made by using those conventions changes the behavior for all stored entities. Now we will show how to override the default ID generation in a more granular way, for particular types of entities.

To override default document identifier generation algorithms, you can register custom conventions per an entity type. You can include your own identifier generation logic.

RegisterIdConvention

public <TEntity> DocumentConventions registerIdConvention(Class<TEntity> clazz, BiFunction<String, TEntity, String> function);
Parameters
function BiFunction<String, TEntity, String> Identifier generation function that supplies a result for given database name (String) and entity object (TEntity).
Return Value
DocumentConventions Current DocumentConventions instance.

Database name parameter

The database name parameter is passed to the register convention methods to allow users to make Id generation decision per database.

Example

Let's say that you want to use semantic identifiers for Employee objects. Instead of employee/[identity] you want to have identifiers like employees/[lastName]/[firstName] (for the sake of simplicity, let us not consider the uniqueness of such identifiers). What you need to do is to create the convention that will combine the employee prefix, LastName and FirstName properties of an employee.

store.getConventions().registerIdConvention(Employee.class,
    (dbName, employee) ->
        String.format("employees/%s/%s", employee.getLastName(), employee.getFirstName()));

Now, when you store a new entity:

try (IDocumentSession session = store.openSession()) {
    Employee employee = new Employee();
    employee.setFirstName("James");
    employee.setLastName("Bond");

    session.store(employee);
    session.saveChanges();
}

the client will associate the employees/Bond/James identifier with it.

Inheritance

Registered conventions are inheritance-aware so all types that can be assigned from registered type will fall into that convention according to inheritance-hierarchy tree.

Example

If we create a new class EmployeeManager that will derive from our Employee class and keep the convention registered in the last example, both types will use the following:

try (IDocumentSession session = store.openSession()) {
    Employee adam = new Employee();
    adam.setFirstName("Adam");
    adam.setLastName("Smith");
    session.store(adam); // employees/Smith/Adam

    EmployeeManager david = new EmployeeManager();
    david.setFirstName("David");
    david.setLastName("Jones");
    session.store(david);  // employees/Jones/David

    session.saveChanges();
}

If we register two conventions, one for Employee and the second for EmployeeManager then they will be picked for their specific types.

store.getConventions().registerIdConvention(Employee.class,
    (dbName, employee) ->
        String.format("employees/%s/%s", employee.getLastName(), employee.getFirstName())
);

store.getConventions().registerIdConvention(EmployeeManager.class,
    (dbName, employee) ->
        String.format("managers/%s/%s", employee.getLastName(), employee.getFirstName())
);

try (IDocumentSession session = store.openSession()) {
    Employee adam = new Employee();
    adam.setFirstName("Adam");
    adam.setLastName("Smith");
    session.store(adam); // employees/Smith/AdamReadBalanceBehavior

    EmployeeManager david = new EmployeeManager();
    david.setFirstName("David");
    david.setLastName("Jones");
    session.store(david);  // managers/Jones/David

    session.saveChanges();
}