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 72F9DD776 for ; Wed, 7 Nov 2012 10:23:05 +0000 (UTC) Received: (qmail 95274 invoked by uid 500); 7 Nov 2012 10:23:05 -0000 Delivered-To: apmail-cayenne-user-archive@cayenne.apache.org Received: (qmail 94994 invoked by uid 500); 7 Nov 2012 10:23:04 -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 94953 invoked by uid 99); 7 Nov 2012 10:23:03 -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 10:23:03 +0000 X-ASF-Spam-Status: No, hits=0.7 required=5.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [59.167.240.32] (HELO fish.ish.com.au) (59.167.240.32) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 07 Nov 2012 10:22:54 +0000 Received: from ip-136.ish.com.au ([203.29.62.136]:49560) by fish.ish.com.au with esmtpsa (TLSv1:CAMELLIA256-SHA:256) (Exim 4.76) (envelope-from ) id 1TW2lv-0005zg-0v for user@cayenne.apache.org; Wed, 07 Nov 2012 21:22:20 +1100 X-CTCH-RefID: str=0001.0A150204.509A365C.01E4,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0 Message-ID: <509A3652.9060202@maniatis.org> Date: Wed, 07 Nov 2012 21:22:10 +1100 From: Aristedes Maniatis User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/17.0 Thunderbird/17.0 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> <509A2A69.1090105@gdcjk.com> In-Reply-To: <509A2A69.1090105@gdcjk.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org It all depends. One usage pattern is to create a long lived "read-only" context which handles the bulk of data in the application which is not editable by users. Then you'll use localObject to copy objects into a new context as needed (perhaps a shopping basket in a web application). In my thick client ROP application, we have one shared context used for all the list views (not editable). But when a user double clicks on a row, we create a new editing context into which they will save any changes. They might have 10 windows open at once, so we we have 10 contexts. When they save a window, we save the context and throw the whole thing away. We also create child contexts for 'sheets'. (Think OSX style dialogs which are modal within a window). For web applications, having a single context which lasts as long as the session is probably quite a common pattern. Ari On 7/11/12 8:31pm, Garth Keesler wrote: > 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 >> >> > -- --------------------------> Aristedes Maniatis GPG fingerprint CBFB 84B4 738D 4E87 5E5C 5EFA EF6A 7D2E 3E49 102A