One of the most important concepts to understand working with a database system is the identification strategy.
In RDBMS separate entities are distinguished with the help of primary keys. Effectively, objects are compared on their field contents and a constrain is used prohibiting 2 different rows in a table to have the same value in a column marked as a primary key.
In the object world, the concept is quite different - any object is unique, independently of the data it holds. For example:
class Car
model
year
car1 = new Car("BMW","1999")
car2 = new Car("BMW","1999")
car1
and car2
objects consist of
the same data, but they are not equal.
On the other hand we can create any amount of references to the object and they will all be equal:
car1 = new Car("BMW","1999")
car2 = car1
car3 = car1
In this example car2
is equal to
car1
and car3
and all these variables actually
reference the same object.
Object database task is to ensure that independently of the way the object was accessed (by different queries or through different navigation access paths) the database must return a reference to the one single object. For a detailed explanation how it is achieved in db4o, please see Unique identity concept.
If you are moving from a relational database to db4o it is very important to understand db4o identity concept. Instead of tying objects to the database reference key you should use the objects, obtained from the database directly without additional worries about their identity.
Unfortunately, there still are some cases, when additional identification system is necessary. This can happen when an object must be referenced out of the application memory boundaries: objects referenced between sessions, objects passed to another application, partially connected applications etc. In these cases, you are encouraged to use db4o internal IDs or Unique Universal IDs (UUID). Internal IDs are unique within one database file and do change after defragment. UUIDs are unique between all db4o databases and do not change. For more information see IDs and UUIDs.
If your application requires autoincremented ID generation you can look at an example implementation in Autoincrement