Hi Joachim, Cayenne [generally--see below] orders by tables (creates a dependency graph), not by rows.
The issue you are referring to is for cyclic relationships where a correct insert order cannot be performed because of the interrelated dependencies. The only solutions that came to my mind (at least for now--open to other ideas) for solving this issue are: 1. Do extra updates after inserts to plug the foreign keys in as they are discovered (could trip up DB constraints, too, but we've already been talking about deferrable constraints in this thread). 2. Split the logic up by PK generators. If you are using sequences (Oracle or PostgreSQL) or the Cayenne PK table (works on all DBs), generate all the PKs (and push them to the FKs) up-front before doing the inserts. If using auto-generated keys (such as MySQL), you are just out of luck. Both approaches would be kind of cumbersome, of course, but certainly doable. Fortunately, cyclic dependency graphs aren't all that common, but when you have them, it is painful. If you (or others) would like to discuss approaches to solve this issue, it would probably best to spin it off into a new thread on entity sorting. The other scenario you mentioned, reflexive relationships where a table references itself, is done by rows and shouldn't be a problem. The entity sorter specifically checks for reflexive relationships and uses a different ordering mechanism. mrg On Wed, Jun 6, 2012 at 9:20 AM, Durchholz, Joachim <[email protected]> wrote: >> I think Cayenne is very intelligent and know the correct >> order to execute the statements, doesn't it? > > It does, but > a) bugs have been reported for the code that does the ordering, so Cayenne > may or may not work for you (I don't know the bug fix status for this one); > b) there is no correct order for interdependent entities. > > I don't know whether Cayenne sorts rows or tables. > E.g. a table with a parent_id column will probably have a table cycle (the > foreign key constraint refers to the same table), but not a row cycle > (there's still a valid order if you commit after each INSERT). If Cayenne > sorts tables, it won't know how to order row inserts/updates/deletes even > though a correct ordering always exists.
