[odb-users] Updating inverse relationship fields

Boris Kolpackov boris at codesynthesis.com
Thu Dec 17 09:52:58 EST 2015


Hi Marcos,

Marcos Gonzalez Menendez <marcos.glez at gmail.com> writes:

> However if we change the state of the in-memory object c1 by calling c1->e_
> = e2, is the programmer responsible of keeping the consistence of vectors
> c1->e_ and c2->e_? I wonder if odb gives a mechanism for doing it
> automatically.

There is not much ODB can do to automate this. And the recommended way
to handle this is by using modifiers that update both sides of the
relationship. For example:

void C::
setE (const odb::lazy_shared_ptr<E>& e)
{
  if (e_ != nullptr)
  {
    // Find the element corresponding to e_ in e_->c_.
    // If e is NULL, remove it, otherwise, set it to e.
  }
  else if (e != nullptr)
  {
    // Add e to e->c_.
  }

  // Set e_ to e.
}

If you think about it, an inverse relationship is really just a single
relationship with two (mirror) sides. So it makes sense to modify it from
a single place.

Boris



More information about the odb-users mailing list