How to use one-to-one relationships

Forum for posting problems using QxOrm library

How to use one-to-one relationships

Postby sgiessl » Wed Aug 01, 2012 8:45 am

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
sgiessl
 
Posts: 3
Joined: Wed Aug 01, 2012 8:40 am

Re: How to use one-to-one relationships

Postby QxOrm admin » Wed Aug 01, 2012 12:15 pm

Hi !

There is a similar topic with some answers to your questions here :
http://www.qxorm.com/forum/phpbb/viewto ... 3b61d10f67

A relation one-to-one is rarely used in a real program.
In almost all cases, you have to define a many-to-one and/or one-to-many relationship, see qxBlog tutorial for examples :
- one-to-many : http://www.qxorm.com/qxorm_en/tutorial.html#tuto_6
- many-to-one : http://www.qxorm.com/qxorm_en/tutorial.html#tuto_7

A one-to-one relationship is like inheritance in object concept : this is when 2 tables share the same ID.
For example, a Person table and a User table could share the same ID into database because a user could be considered as a person => this is a one-to-one relationship.

Note : be careful, use qx::dao::create_table<T> function only for prototypes !
To create your database schema, you have to write your own function using introspection engine of QxOrm library.
It's very easy to do, you can find 2 examples on the FAQ of QxOrm library here :
- http://www.qxorm.com/qxorm_en/faq.html#faq_230
- qx::QxClassX::dumpSqlSchema() method : http://www.qxorm.com/qxorm_en/resource/ ... chema.html
QxOrm admin
 

Re: How to use one-to-one relationships

Postby sgiessl » Sat Aug 04, 2012 12:44 pm

Hi,

thank you very much for the useful answer. (Sorry for asking the same question even though it has already been answered.)

Thanks for your work on QxOrm, it is appreciated very much.

Sandro
sgiessl
 
Posts: 3
Joined: Wed Aug 01, 2012 8:40 am


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 1 guest

cron