You are here: Platform Specific Issues > Disconnected Objects > Comparison Of Different IDs > Example UUID

Example UUID

This example demonstrates how you can use UUIDs to identify objects across objects containers. Take a look advantages and disadvantages of UUIDs: See "Comparison Of Different IDs"

Don't forget to add UUID support.

This example assumes that all object have a common super class, IDHolder, which holds the UUID in a field.

private final UUID uuid = UUID.randomUUID();

public UUID getObjectId(){
    return uuid;
}
IDHolder.java: generate the id

It's important to index the id-field, otherwise looking up for object by id will be slow.

configuration.common().add(new UuidSupport());
configuration.common().objectClass(IDHolder.class).objectField("uuid").indexed(true);
UuidOnObject.java: index the uuid-field

The id is hold by the object itself, so you can get it directly.

IDHolder uuidHolder = (IDHolder)obj;
UUID uuid = uuidHolder.getObjectId();
UuidOnObject.java: get the uuid

You can get the object you can by a regular query.

Query query = container.query();
query.constrain(IDHolder.class);
query.descend("uuid").constrain(idForObject);
IDHolder object= (IDHolder) query.execute().get(0);
UuidOnObject.java: get an object its UUID