[odb-users] Updating inverse relationship fields
Marcos Gonzalez Menendez
marcos.glez at gmail.com
Wed Dec 16 18:41:41 EST 2015
Hi,
I would like to confirm the behavior of odb library regarding the state of
inverse relationships fields.
I'll explain it through an example. Let's put that we have the following
classes:
#pragma db object
class E
{
public:
E (QString n, QString d) : name_(n), description_(d)
{
}
const QString& name() const {return name_;}
const QString& description() const { return description_; }
void setName(const QString& v) {name_ = v;}
void setDescription(const QString& v) {description_ = v;}
int id() const {return id_;}
private:
E() {}
friend class odb::access;
public:
#pragma db id auto
int id_;
#pragma db not_null
QString name_;
QString description_;
#pragma db inverse(e_)
QList<odb::lazy_weak_ptr<C>> c_;
};
#pragma db object
class C
{
public:
C (QString n, QString d, std::shared_ptr<E> e) :
name_(n), description_(d), e_(e)
{
}
const QString& name() const {return name_;}
const QString& description() const { return description_; }
void setName(const QString& v) {name_ = v;}
void setDescription(const QString& v) {description_ = v;}
int id() const {return id_;}
public:
C() {}
friend class odb::access;
public:
#pragma db id auto
int id_;
#pragma db not_null
QString name_;
QString description_;
#pragma db not_null
odb::lazy_shared_ptr<E> e_;
};
int main(int argc, char *argv[])
{
transaction t(db->begin());
shared_ptr<E> e1(db->load<E>(1));
shared_ptr<E> e2(db->load<E>(2));
shared_ptr<C> c1(db->load<C>(1));
// c1->e_ points to e1
c1->e_ = e2;
// Now: c1->e_ points to e2
db->update(c1);
t.commit();
}
When we load objects c1 and c2, odb will populate automatically the vector
e1->c_ and the vector e2->c_.
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. Of course, we
have the reload method but it implies database connectivity and it would be
better a faster solution using only in-memory operations.
So, what options do we have to achieve consistence for the in-memory
objects? Is writing custom code the only option?
Thanks in advance for any help you can provide
---
Marcos Gonzalez
More information about the odb-users
mailing list