Preserve many-to-one mapping in mapped objects as well

Forum for posting problems using QxOrm library

Preserve many-to-one mapping in mapped objects as well

Postby shaan7 » Mon Apr 28, 2014 5:34 pm

In my usecase, multiple Media (songs) can have the same Artist, hence a many to one relationship defined like this-

Code: Select all
template <> void register_class(QxClass<Artist> & a)
{
    a.id(& Artist::m_name, "name");

    a.relationOneToMany(& Artist::m_media, "media", "artist_id");
}


The relation works as I can see from the database. However, when I am trying to load the media, even though they are related to the same Artist the two Artist objects are different (even though the names are same).
Consider this test-

Code: Select all
void test()
{
    qx::dao::create_table<Album>();
    qx::dao::create_table<Artist>();
    qx::dao::create_table<Media>();

    QSharedPointer<Artist> artist(new Artist("Shaan"));

    QSharedPointer<Media> media1(new Media("uri://media1"));
    QSharedPointer<Media> media2(new Media("uri://media2"));

    media1->setArtist(artist);
    media2->setArtist(artist);

    qx::dao::insert(media1);
    qx::dao::insert(media2);

    QSharedPointer<Media> fetchedMedia1(new Media("uri://media1"));
    QSharedPointer<Media> fetchedMedia2(new Media("uri://media2"));

    typedef qx::QxCollection< QString, QSharedPointer<Media> > MediaList;
    MediaList medias;
    qx::dao::fetch_all_with_all_relation(medias);

    _foreach (QSharedPointer<Media> media, medias) {
        qDebug() << media->artist().data() << media->artist()->name();
    }
}


This outputs-

Code: Select all
0x2357f80 "Shaan"
0x23513b0 "Shaan"


Is there a way I can ask QxOrm that the two Media to point to the same object of Artist in this case? I'd want the output of the above program to be this-

Code: Select all
0x2357f80 "Shaan"
0x2357f80 "Shaan"
shaan7
 
Posts: 3
Joined: Sun Apr 27, 2014 2:59 pm

Re: Preserve many-to-one mapping in mapped objects as well

Postby qxorm » Mon Apr 28, 2014 6:47 pm

Hello,

Is there a way I can ask QxOrm that the two Media to point to the same object of Artist in this case?

No, even if the fetched items is the same object in database, QxOrm library will create a new instance.

But you can easily manage it yourself using QxCache module !
More details about qx::cache here :
- http://www.qxorm.com/qxorm_en/faq.html#faq_220
- http://www.qxorm.com/doxygen/html/group___qx_cache.html
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: Preserve many-to-one mapping in mapped objects as well

Postby honrel » Mon Aug 18, 2014 2:36 pm

qxorm wrote:Hello,

Is there a way I can ask QxOrm that the two Media to point to the same object of Artist in this case?

No, even if the fetched items is the same object in database, QxOrm library will create a new instance.

But you can easily manage it yourself using QxCache module !
More details about qx::cache here :
- http://www.qxorm.com/qxorm_en/faq.html#faq_220
- http://www.qxorm.com/doxygen/html/group___qx_cache.html


Hello, I have а similar problem?
I need to implement stateful session.
Great solution is to use RAII such as odb orm (http://www.codesynthesis.com/products/o ... xhtml#11.1).

Code: Select all
void test()
{
    qx::dao::create_table<Album>();
    qx::dao::create_table<Artist>();
    qx::dao::create_table<Media>();

    {
        QSharedPointer<Artist> artist(new Artist("Shaan"));

        QSharedPointer<Media> media1(new Media("uri://media1"));
        QSharedPointer<Media> media2(new Media("uri://media2"));

        media1->setArtist(artist);
        media2->setArtist(artist);

        qx::dao::insert(media1);
        qx::dao::insert(media2);

        QSharedPointer<Media> fetchedMedia1(new Media("uri://media1"));
        QSharedPointer<Media> fetchedMedia2(new Media("uri://media2"));

        qx::dao::fetch_by_id_with_all_relation(fetchedMedia1);
        qx::dao::fetch_by_id_with_all_relation(fetchedMedia1);

        qAssert(media1 == fetchedMedia1);
        qAssert(media2 == fetchedMedia2);
        qAssert(fetchedMedia1->artist() == artist);
        qAssert(fetchedMedia1->artist()->media() == fetchedMedia1);
    }
}

Is it possible? Please help me!
honrel
 
Posts: 1
Joined: Mon Aug 18, 2014 12:24 pm

Re: Preserve many-to-one mapping in mapped objects as well

Postby qxorm » Mon Aug 18, 2014 7:08 pm

Hello,

Is it possible?

QxOrm library will create new instances each time you fetch something.

If you want to keep something in memory, and then check later if the instance exist or not, you have to use the QxCache module.
More details here :
- http://www.qxorm.com/qxorm_en/faq.html#faq_220
- http://www.qxorm.com/doxygen/html/group___qx_cache.html
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 10 guests

cron