Return-Path: X-Original-To: apmail-cayenne-user-archive@www.apache.org Delivered-To: apmail-cayenne-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D4861D60D for ; Wed, 7 Nov 2012 09:31:55 +0000 (UTC) Received: (qmail 65754 invoked by uid 500); 7 Nov 2012 09:31:55 -0000 Delivered-To: apmail-cayenne-user-archive@cayenne.apache.org Received: (qmail 65677 invoked by uid 500); 7 Nov 2012 09:31:55 -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 65633 invoked by uid 99); 7 Nov 2012 09:31:53 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 07 Nov 2012 09:31:53 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [216.180.54.1] (HELO fly.hiwaay.net) (216.180.54.1) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 07 Nov 2012 09:31:43 +0000 Received: from [10.20.30.64] ([216.180.54.218]) by fly.hiwaay.net (8.13.8/8.13.8/fly) with ESMTP id qA79VL3S005436 for ; Wed, 7 Nov 2012 03:31:22 -0600 Message-ID: <509A2A69.1090105@gdcjk.com> Date: Wed, 07 Nov 2012 03:31:21 -0600 From: Garth Keesler User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121028 Thunderbird/16.0.2 MIME-Version: 1.0 To: user@cayenne.apache.org Subject: Re: setToOneTarget not works for two transient objects (3.1B1) References: <5099F2BB.30606@maniatis.org> <509A0509.2090707@maniatis.org> In-Reply-To: <509A0509.2090707@maniatis.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org Is the general case that a context typically lasts as long as the transaction, gets discarded, and a new one is created for the next transaction or is it dependent on usage in the program? For example, a web app as discussed here discards contexts based on user interactions while a thick client app would tend to reuse the same context. Just curious... Garth On 11/07/2012 12:51 AM, Aristedes Maniatis wrote: > On 7/11/12 4:57pm, Alexey Katorgin wrote: >>> >>> You should always create a context and then create objects within >>> that context >> using the methods Cayenne >>> gives you. This is different to the undecorated POJO that Hibernate >>> allows you >> to create. >>> >>> Ari >>> >> >> >> I develop a web-app and if I will create entities bounded to a >> dataContext, and >> after user has leaved the page (by browser navigation buttons, >> closing the tab, >> etc.) such entities will stay uncommitted and may be in inconsistency >> state >> (user has not filled all required fields). And on the next commit >> this objects >> will be committed and raise validation exception. >> >> It is the reason why I want to use unbounded dataObjects. >> >> So I've decide to use transient objects not bounded to any >> dataContext. In this >> case such object just be removed by garbage collector after user >> leaved page >> unexpectedly. And where user will click OK, such objects will be >> manually >> attached to a dataContext and committed. >> >> It makes my code less clearly if can't set relationships between >> transient >> objects. Because I need to create additional temporary lists to store >> all >> transient entities and iterate its to register to a dataContext before >> committing. >> >> >> What is the best practice for this use case? > > > Typically you bind a context to the user session so that the user can > continue to navigate through the site and not lose the work they have > done but not yet saved. For example, they may assemble a shopping > basket, contact details and payment information. Then you commit the > whole thing atomically at the end. > > Don't think of a data context as something you create when you are > ready to save. Think of it as a big bucket into which you put all the > objects you are working with. At the end, throw it away and make a new > one, or commit it to the database. For more complex arrangements read > up on how to create parent/child contexts, but you don't need to do > that here. Start simple. > > Make as many contexts as you need to have buckets of different > information. > > > Ari > >