1

Assuming you are using DB4O with the standard configuration and out-of-the-box - meaning, you are using DB4O's standard internal id scheme, as per below**.

First, as an assumption, when updating or inserting an object into the database, the programmer only has one option, to call "objectContainer.store(object)".

Now, how does DB4O know whether to update an existing object in the DB, or to insert an entirely new one? My guess is that DB4O looks to see if the internal id of the object is not null.

However, I could also see the possibility that the burden is on the programmer to first pull the object from the DB, then update it. So my question is, how can we reliably update an object in a DB4O database without first pulling it into Java memory?

*This DB4O article doesn't really answer my question

http://community.versant.com/Documentation/Reference/db4o-8.1/java/reference/Content/basics/update_concept.htm

**This is very informative:

http://community.versant.com/documentation/Reference/db4o-8.1/java/reference/Content/platform_specific_issues/disconnected_objects/comparison_of_ids.htm

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

1

db4o uses object identity in order to decide whether to insert / update objects (and AFAIK this is a central concept in db4o) so, even though it is possible, there's no easy way to accomplish that at db4o level (you probably have an abstraction for your data layer access; what you can do is to have two methods in this layer: insert and update; in update, before calling db4o.store(), you can check whether the object has already been loaded or not. This way, at least you can avoid mistakes that would cause object duplication in your db. If you do the same thing in insert you can be sure that you'll not update an object by mistake.

Hope this help.

Vagaus
  • 4,174
  • 20
  • 31
  • Thanks Vagaus. I think I will just have one extra boolean value per model object. If I pulled the object from the database, then saved = true, if I created the object in Java, saved = false. It might be that easy. – Alexander Mills Sep 19 '13 at 11:57
  • I understand your abstraction layer idea, but the problem is that at the core you only have one method i.e. store(object), therefore, all you can do is make sure you are storing the *correct* object. And the way to ensure that you have the correct object seems to me to be to mark the objects that have come from the DB yourself. However, I would be interested to know if DB4O marks these objects with the internal id. So I am not sure if I even need the saved boolean value, just check to see if internal id is null or not null. – Alexander Mills Sep 19 '13 at 11:58
  • with that said, shouldn't DB4O *know* that an object has been loaded from the DB if its internal id is not null....? – Alexander Mills Sep 19 '13 at 14:58
  • 1
    @Alex, thats exactly what I suggested (let db4o tell you if it knows about the object). You can use getID() method from ExtObjectContainer interface (http://community.versant.com/documentation/reference/db4o-8.0/java/api/com/db4o/ext/ExtObjectContainer.html#getID(java.lang.Object) You'll do something like: db.ext().getID(myObj) – Vagaus Sep 19 '13 at 18:01