Return-Path: Delivered-To: apmail-cayenne-user-archive@www.apache.org Received: (qmail 19527 invoked from network); 21 Nov 2008 11:50:34 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 21 Nov 2008 11:50:34 -0000 Received: (qmail 49403 invoked by uid 500); 21 Nov 2008 11:50:42 -0000 Delivered-To: apmail-cayenne-user-archive@cayenne.apache.org Received: (qmail 49391 invoked by uid 500); 21 Nov 2008 11:50:41 -0000 Mailing-List: contact user-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@cayenne.apache.org Delivered-To: mailing list user@cayenne.apache.org Received: (qmail 49380 invoked by uid 99); 21 Nov 2008 11:50:41 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Nov 2008 03:50:41 -0800 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [208.78.103.231] (HELO vorsha.objectstyle.org) (208.78.103.231) by apache.org (qpsmtpd/0.29) with SMTP; Fri, 21 Nov 2008 11:49:18 +0000 Received: (qmail 2615 invoked from network); 21 Nov 2008 11:50:03 -0000 Received: from unknown (HELO ?IPv6:::1?) (127.0.0.1) by localhost with SMTP; 21 Nov 2008 11:50:03 -0000 Message-Id: From: Andrus Adamchik To: user@cayenne.apache.org In-Reply-To: <49249CE8.1020303@nitido.com> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v929.2) Subject: Re: Order of commits? Date: Fri, 21 Nov 2008 13:50:00 +0200 References: <476E5BA4-E9D6-46F7-AB2C-437135F2E70F@objectstyle.org> <49244D72.7070004@genux.fr> <49249CE8.1020303@nitido.com> X-Mailer: Apple Mail (2.929.2) X-Virus-Checked: Checked by ClamAV on apache.org The problem is likely in the mapping. At the minimum you'll need to check that PK Generation Strategy is set to "Database-Generated" to all entities in question. Andrus On Nov 20, 2008, at 1:10 AM, Michael Shea wrote: > Hi guys - > > I am wondering if there is any way to tell cayenne what order to > commit new objects in. > > My issue is basically like this: > > Suppose I have Category objects and Item objects. > > A user can pass in a CategoryDescriptor, which contains a list of > ItemDescriptors. I want cayenne to generate, from these descriptors, > the corresponding database-model objects and rows in the database. > So I do something like this: > > public Category createCategory( CategoryDescriptor > categoryDescriptor ) > { > Context context = ...; // do something to get the context here. > Category category = context.newObject( Category.class ); > .... // fill in stuff about the category object here. > for ( ItemDescriptor itemDescriptor : > categoryDescriptor.getItemDescriptors() ) > { > Item item = context.newObject( Item.class ); > ... // fill in stuff about the item object here. > } > context.commitChanges(); > return Category; > } > > The problem I am having is that both Category and Item have primary > keys which are autogenerated by database (I am using MySQL, the ID's > are generated via AUTO_INCREMENT columns). It looks like cayenne is > attempting to create the Item objects before creating the Category > object, and then it's giving me a commit exception like this: > > org.apache.cayenne.CayenneRuntimeException: [v.3.0M4 May 18 2008 > 16:32:02] Commit Exception > at > org.apache.cayenne.access.DataContext.flushToParent(DataContext.java: > 1192) > at > org.apache.cayenne.access.DataContext.commitChanges(DataContext.java: > 1066) > at com.nitido.rss.RSSNugget.createCategory(RSSNugget.java:294) > ... > Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: > Table 'rss.AUTO_PK_SUPPORT' doesn't exist > at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026) > ... > > It looks like, since the ID on the Item is null and the Category > object hasn't been initialized yet, Cayenne is trying to generate an > ID for the row. I don't see any other INSERT commands from the > logger after I call context.commitChanges(), so the attempt to > insert a row for the Item object looks to be the first thing that > it's trying to do. > > So, my questions are: > 1. Is this normal? Or does this sound like it could be a problem > caused by me screwing up the mapping.xml? > 3. Is it a good idea to work around this by using either > AUTO_PK_SUPPORT or a custom database sequence, and forget about the > AUTO_INCREMENT feature? I have control over the database schema, so > I could do this instead. Alternately, I could just generate my own > ID's without using cayenne, but I'd rather not. > 4. Is it normal to do this by committing the Category objects before > attempting to create Item objects related to them? That seems > somewhat hacky to me, though. > > BTW, I'm using Cayenne 3.0M4. > > Thanks! > > > Shea. >