[odb-users] How to persist cyclic pointers among A->B->C->A?

Justin Huang yohuang at nvidia.com
Mon Oct 14 10:59:30 EDT 2019


>> One way would be to use the session (object cache) and check if each object is already there before persisting it:

Is there concrete example? I guess I will need to use 'session s;' and use db->load(<object_id>) or s.cache_find(db, <object_id>) to test if the object has been persisted?

-----Original Message-----
From: Justin Huang 
Sent: Monday, October 14, 2019 9:47 PM
To: odb-users at codesynthesis.com
Subject: RE: [odb-users] How to persist cyclic pointers among A->B->C->A?

Hi Boris,

Thanks for quick response!

>> You can persist them in any order (within the same transaction) provided their ids have been assigned (which could be problematic if you use auto- assigned ids).
I am using '#pragma db id auto' currently and got 'foreign key constraint failed' error at runtime. I think persistence in this case is order dependent, and I will try to use '#pragma db id' instead and assign 'id' manually.

For this kind of pointer chain loop, do I need to set any 'inverse' data member pragma?

If I have X -> Y, Y -> X bidirectional relationship, what's the impact if I do not use the 'inverse' pragma ( I manually set 'id')?

>> One way would be to use the session (object cache) and check if each object is already there before persisting it:
Good idea. I'll check examples in the manual on how to use it. Currently I am throwing exception using pre-persistence check for each db->persist(object) and ignore the exception which seems avoided duplicated entry in the table. 

Thanks,
Justin

-----Original Message-----
From: Boris Kolpackov <boris at codesynthesis.com>
Sent: Monday, October 14, 2019 9:34 PM
To: Justin Huang <yohuang at nvidia.com>
Cc: odb-users at codesynthesis.com
Subject: Re: [odb-users] How to persist cyclic pointers among A->B->C->A?

Justin Huang <yohuang at nvidia.com> writes:

> If I have three classes A, B and C, and A has pointer to object of 
> class B, B has pointer to object of C, and C has pointer to object of 
> A, which is a cyclic pointer relationship.
> 
> How should I persist objects a1/a2/b1/c1? E.g., the persistent 
> sequence, and how to avoid persist same objects twice?

You can persist them in any order (within the same transaction) provided their ids have been assigned (which could be problematic if you use auto- assigned ids).

You will also have to somehow make sure you are not persising the same object twice. One way would be to use the session (object cache) and check if each object is already there before persisting it:

https://codesynthesis.com/products/odb/doc/manual.xhtml#11
-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain
confidential information.  Any unauthorized review, use, disclosure or distribution
is prohibited.  If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------



More information about the odb-users mailing list