Dear QxOrm developers,
I'm wondering how to use the relationship which is modeled by
QxClass<T>::relationOneToOne(). Unfortunately there seems to be a
lack of documentation, so I'm hoping someone can give me some
useful pointers to get started. I hope the question doesn't sound
too silly, I'm new to ORM frameworks.
My goal is having two classes A and B where A contains a pointer to B, i.e.:
class B {
long int m_id;
[...]
}
class A {
long int m_id;
QSharedPointer<B> m_b;
[...]
}
now, when the mapping for A is registered, I use something like
namespace qx {
template <> void register_class(QxClass<B> & t)
{
t.id(&B::m_id, "id", 0);
[...]
}}
namespace qx {
template <> void register_class(QxClass<A> & t)
{
t.id(&A::m_id, "id", 0);
t.relationOneToOne(& A::m_b, "bId");
[...]
}}
Now my question is, how to save and fetch objects of class A, including relation B?
I noticed that, when using qx::dao::create_table, the schema of
class A does not contain any attribute 'bId' - And when using
fetch_by_xxx_with_all_relations(), the generated query will do a
join on both primary key attributes 'id'.
So if QxOrm always assumes that the relationship is specified by
primary keys, why is there a 'const QString & sKey' parameter in
QxClass<T>::relationOneToOne() at all? I'm sure I'm missing
something.
Also, if my observation is correct, how can an object A be saved
to the database? I tried something like the following:
session.save(aObject);
aObject->m_b->m_id = aObject->m_id; // (2)
session.save(bObject);
However, the B::m_id is auto generated, so (2) does not have any effect. Therefore, the relationships can be messed up, i.e. if the database already contains two A objects (IDs 1, 2), one B object (ID 1), and I try to store another A (and B) object. The resulting IDs of A and B will be 3 and 2 respectively. Therefore, the new B will be accidentally associated with an old A object.
I'm looking forward to your answers. Best regards, Sandro