[odb-users] odb-2.2.1: calling db->persist() twice on the the instance results in two database entries

Boris Kolpackov boris at codesynthesis.com
Sat Mar 2 06:27:49 EST 2013


Hi Hugo,

Hugo.Mildenberger at web.de <Hugo.Mildenberger at web.de> writes:

> 		#pragma   db id auto
> 		std::size_t _id;

You have a persistent class with automatically-assigned id. This means
that every time you persist an instance of this class, ODB will assign
a new id, which by design cannot conflict with any existing id.

I anticipate you will suggest that ODB should somehow recognize that the
id has been assigned by a previous call to persist(). Note, however, that
there is no way for ODB to know whether the value in the _id member is a
valid object id or some uninitialized garbage.

It is also pretty easy to make a check like this yourself. For example,
you could initialize the _id member to 0 for transient objects (the 
assigned id can never be 0) and then check that it is 0 in the
pre_persist callback. For example:

#pragma db object callback(check)
class telecom 
{
public:
  telecom (): _id (0) {}

  void
  check (odb::callback_event e, odb::database&) const
  {
    if (e == odb::callback_event::pre_persist)
      if (_id != 0)
        throw odb::object_already_persistent ();
  }

  ...

  #pragma db id auto
  std::size_t _id;
};

Boris



More information about the odb-users mailing list