You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Hugi Thordarson <hu...@karlmenn.is> on 2018/11/01 14:35:42 UTC

Controlling object insert order

Hi all.
I have a set of new (uncommitted) objects that I need to insert into the DB in a certain order. For… Reasons. (let's hear it for legacy DBs with meaningful PKs, yay!)

The action is not performance sensitive and the whole thing occurs within a transaction, so performing multiple commits is OK. My initial though was that when saving, I could just iterate through the new objects, localizing each one in a new ObjectContext and committing that on each iteration—but I haven't found a way to get that to work.

Any ideas?

Cheers,
- hugi

Re: Controlling object insert order

Posted by Hugi Thordarson <hu...@karlmenn.is>.
Yeah, it's… bad. I do have the relationships mapped out, although they aren't complemented by FKs in the DB. But like you, I'm impressed by how well Cayenne makes the best of a bad situation. And it will make this customer's scheduled migration away from meaningful keys much easier.

- hugi


> On 1 Nov 2018, at 16:57, Lon Varscsak <lo...@gmail.com> wrote:
> 
> lol, my whole DB is meaningful primary keys...yay us!  So far, Cayenne's
> default logic has handled it without exception, but I've got all of the
> relationships mapped out.  I'm assuming your situation is different, but I
> had to jump in and give a hooray for meaningful PKs. :P
> 
> On Thu, Nov 1, 2018 at 7:58 AM John Huss <jo...@gmail.com> wrote:
> 
>> Without having more details my general recommendation would be to handle
>> the details up front before you get to the Cayenne level. Put things in
>> order in memory and then create your Cayenne Objects and commit them one at
>> a time.
>> 
>> On Thu, Nov 1, 2018 at 9:35 AM Hugi Thordarson <hu...@karlmenn.is> wrote:
>> 
>>> Hi all.
>>> I have a set of new (uncommitted) objects that I need to insert into the
>>> DB in a certain order. For… Reasons. (let's hear it for legacy DBs with
>>> meaningful PKs, yay!)
>>> 
>>> The action is not performance sensitive and the whole thing occurs within
>>> a transaction, so performing multiple commits is OK. My initial though
>> was
>>> that when saving, I could just iterate through the new objects,
>> localizing
>>> each one in a new ObjectContext and committing that on each
>> iteration—but I
>>> haven't found a way to get that to work.
>>> 
>>> Any ideas?
>>> 
>>> Cheers,
>>> - hugi
>> 


Re: Controlling object insert order

Posted by Lon Varscsak <lo...@gmail.com>.
lol, my whole DB is meaningful primary keys...yay us!  So far, Cayenne's
default logic has handled it without exception, but I've got all of the
relationships mapped out.  I'm assuming your situation is different, but I
had to jump in and give a hooray for meaningful PKs. :P

On Thu, Nov 1, 2018 at 7:58 AM John Huss <jo...@gmail.com> wrote:

> Without having more details my general recommendation would be to handle
> the details up front before you get to the Cayenne level. Put things in
> order in memory and then create your Cayenne Objects and commit them one at
> a time.
>
> On Thu, Nov 1, 2018 at 9:35 AM Hugi Thordarson <hu...@karlmenn.is> wrote:
>
> > Hi all.
> > I have a set of new (uncommitted) objects that I need to insert into the
> > DB in a certain order. For… Reasons. (let's hear it for legacy DBs with
> > meaningful PKs, yay!)
> >
> > The action is not performance sensitive and the whole thing occurs within
> > a transaction, so performing multiple commits is OK. My initial though
> was
> > that when saving, I could just iterate through the new objects,
> localizing
> > each one in a new ObjectContext and committing that on each
> iteration—but I
> > haven't found a way to get that to work.
> >
> > Any ideas?
> >
> > Cheers,
> > - hugi
>

Re: Controlling object insert order

Posted by Hugi Thordarson <hu...@karlmenn.is>.
Hi John,
thanks for the input—yeah, that would have been one way. Unfortunately that would have required a bit of a major rewrite of existing functionality which is heavily based on business logic the DataObjects.

Ended up inserting the same number of empty rows as the ordered objects were, fetching them back ordered by the DB-generated PK, and populating the empty rows with the final data. I'll then deal with the resulting emotional issues in therapy.

Cheers,
- hugi


> On 1 Nov 2018, at 14:58, John Huss <jo...@gmail.com> wrote:
> 
> Without having more details my general recommendation would be to handle
> the details up front before you get to the Cayenne level. Put things in
> order in memory and then create your Cayenne Objects and commit them one at
> a time.
> 
> On Thu, Nov 1, 2018 at 9:35 AM Hugi Thordarson <hu...@karlmenn.is> wrote:
> 
>> Hi all.
>> I have a set of new (uncommitted) objects that I need to insert into the
>> DB in a certain order. For… Reasons. (let's hear it for legacy DBs with
>> meaningful PKs, yay!)
>> 
>> The action is not performance sensitive and the whole thing occurs within
>> a transaction, so performing multiple commits is OK. My initial though was
>> that when saving, I could just iterate through the new objects, localizing
>> each one in a new ObjectContext and committing that on each iteration—but I
>> haven't found a way to get that to work.
>> 
>> Any ideas?
>> 
>> Cheers,
>> - hugi


Re: Controlling object insert order

Posted by John Huss <jo...@gmail.com>.
Without having more details my general recommendation would be to handle
the details up front before you get to the Cayenne level. Put things in
order in memory and then create your Cayenne Objects and commit them one at
a time.

On Thu, Nov 1, 2018 at 9:35 AM Hugi Thordarson <hu...@karlmenn.is> wrote:

> Hi all.
> I have a set of new (uncommitted) objects that I need to insert into the
> DB in a certain order. For… Reasons. (let's hear it for legacy DBs with
> meaningful PKs, yay!)
>
> The action is not performance sensitive and the whole thing occurs within
> a transaction, so performing multiple commits is OK. My initial though was
> that when saving, I could just iterate through the new objects, localizing
> each one in a new ObjectContext and committing that on each iteration—but I
> haven't found a way to get that to work.
>
> Any ideas?
>
> Cheers,
> - hugi