You are here: Advanced Features > IDs And UUIDs

IDs and UUIDs

The db4o team recommends not to use object IDs where it is not necessary. db4o keeps track of object identities in a transparent way, by identifying "known" objects on updates. See "Identity Concept"

If you really need to have ids for you're object, take a look at this comparison. See "Comparison Of Different IDs"

Internal IDs

Each object, except value objects like ints, floats or string, do have an internal id. This id is unique within on db4o database and db4o uses it internally for managing the objects. However you also can access this id or retrieve objects by the internal id.

You can get the internal id of an object from the extended object container.

long interalId = container.Ext().GetID(obj);
Db4oInternalIdExample.cs: get the db4o internal ids
Dim interalId As Long = container.Ext().GetID(obj)
Db4oInternalIdExample.vb: get the db4o internal ids

And you can retrieve an object by the internal id. Note that when you get an object by its internal id that it won't be activated. Therefore you have to explicitly activate the object.

long internalId = idForObject;
object objectForID = container.Ext().GetByID(internalId);
// getting by id doesn't activate the object
// so you need to do it manually
container.Ext().Activate(objectForID);
Db4oInternalIdExample.cs: get an object by db4o internal id
Dim internalId As Long = idForObject
Dim objForID As Object = container.Ext().GetByID(internalId)
' getting by id doesn't activate the object
' so you need to do it manually
container.Ext().Activate(objForID)
Db4oInternalIdExample.vb: get an object by db4o internal id

db4o UUIDs

db4o can also generate a UUIDs for each object. The UUIDs main purpose is to enable replication. By default db4o doesn't assign a UUID to each object. You have to enable this globally or for certain types. For example:

IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.File.GenerateUUIDs = ConfigScope.Globally;
FileConfiguration.cs: Enable db4o uuids globally
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
configuration.File.GenerateUUIDs = ConfigScope.Globally
FileConfiguration.vb: Enable db4o uuids globally

A db4o UUID consists of two parts. The first part is the database signature which is unique to the database.

The second part a unique id within the object-container for the object. Both parts together represent a unique id.

You can get the db4o uuid from the extended object container.

Db4oUUID uuid = container.Ext().GetObjectInfo(obj).GetUUID();
Db4oUuidExample.cs: get the db4o-uuid
Dim uuid As Db4oUUID = container.Ext().GetObjectInfo(obj).GetUUID()
Db4oUuidExample.vb: get the db4o-uuid

And you can get an object by its UUID. Note that when you get an object by its UUIDthat it won't be activated. Therefore you have to explicitly activate the object.

object objectForId = container.Ext().GetByUUID(idForObject);
// getting by uuid doesn't activate the object
// so you need to do it manually
container.Ext().Activate(objectForId);
Db4oUuidExample.cs: get an object by a db4o-uuid
Dim objForId As Object = container.Ext().GetByUUID(idForObject)
' getting by uuid doesn't activate the object
' so you need to do it manually
container.Ext().Activate(objForId)
Db4oUuidExample.vb: get an object by a db4o-uuid