You are here: Basics Operations & Concepts > Identity Concept

Identity Concept

You've maybe noticed that you don't need to add an identifier to your objects in order to store them with db4o. So how does db4o manage objects? db4o uses the object-identity to identify objects. db4o ensures that each stored object in the database has only one in memory representation per object container. If you load an object in different ways db4o will always return the same object. Or as rule of thumb: The objects in the database behave like objects in memory. When you run multiple queries or retrieve objects in another way, the same object in the database will always represented by the same object in memory.

Car theCar = container.Query<Car>()[0];
Pilot thePilot = container.Query<Pilot>()[0];
Pilot pilotViaCar = theCar.Pilot;
AssertTrue(thePilot == pilotViaCar);
IdentityConcepts.cs: db4o ensures reference equality
Dim theCar As Car = container.Query(Of Car)()(0)
Dim thePilot As Pilot = container.Query(Of Pilot)()(0)
Dim pilotViaCar As Pilot = theCar.Pilot
IdentityConcepts.vb: db4o ensures reference equality

In order to implement this behavior each object container keeps a mapping between the objects in memory and the stored object representation. When you load the same object with multiple object-containers (for example with session-containers or in client-server-mode), it will have different in memory-identity. db4o ensures the same identity only for a single object-container.

Car loadedWithContainer1 = container1.Query<Car>()[0];
Car loadedWithContainer2 = container2.Query<Car>()[0];
AssertFalse(loadedWithContainer1 == loadedWithContainer2);
IdentityConcepts.cs: Loading with different object container results in different objects
Dim loadedWithContainer1 As Car = container1.Query(Of Car)()(0)
Dim loadedWithContainer2 As Car = container2.Query(Of Car)()(0)
IdentityConcepts.vb: Loading with different object container results in different objects

This also means that an object should always be processed with the same object-container. When you load a object in one container and store it with another container db4o cannot recognize the object and will store it as a completely new object. Therefore you need to use the same container to store and load objects.

The identity concept works really well for desktop and embedded applications where you can have a single object container and keep that container open while the application is running. In such a case the behavior is just like you would work with regular objects. However this behavior doesn't work where you need to serialize objects, for example in web-applications. In such scenarios you need to do some extra work. See "Disconnected Objects"

Further Information

Maybe your wondering why db4o manages object by identity. Why not by equality? There are good reasons why this is the case. See "Identity Vs Equality"

In order to manage objects by identity db4o has a reference cache which contains all loaded objects. See "The Reference Cache"