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 C0E00188D5 for ; Fri, 11 Sep 2015 16:39:02 +0000 (UTC) Received: (qmail 54092 invoked by uid 500); 11 Sep 2015 16:39:02 -0000 Delivered-To: apmail-cayenne-user-archive@cayenne.apache.org Received: (qmail 54067 invoked by uid 500); 11 Sep 2015 16:39:02 -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 54055 invoked by uid 99); 11 Sep 2015 16:39:02 -0000 Received: from Unknown (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Sep 2015 16:39:02 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id E14B1E6B69 for ; Fri, 11 Sep 2015 16:39:01 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.001 X-Spam-Level: * X-Spam-Status: No, score=1.001 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id PHmrgroa4BJ7 for ; Fri, 11 Sep 2015 16:38:48 +0000 (UTC) Received: from smtp4.objectstyle.com (smtp4.objectstyle.com [23.253.156.183]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with ESMTP id BCDC320103 for ; Fri, 11 Sep 2015 16:38:47 +0000 (UTC) Received: from [192.168.4.122] (unknown [212.98.191.4]) by smtp4.objectstyle.com (Postfix) with ESMTPSA id 42FA8CC358 for ; Fri, 11 Sep 2015 16:41:01 +0000 (UTC) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) Subject: Re: Query at the time of inserting data using Apache Cayenne From: Andrus Adamchik In-Reply-To: Date: Fri, 11 Sep 2015 19:38:46 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: <0FFA652A-6806-455F-91C5-F68DAE192A6D@objectstyle.org> References: To: user@cayenne.apache.org X-Mailer: Apple Mail (2.2104) Ari's example was idiomatic Cayenne, even though it did not contain the = solution. I.e. it is generally a good idea to create and register a new = object in one place with context.newObject(..), before you start = modifying object properties. Anyways...=20 >> I have solved the problem by changing my pk generation strategy in = cayenne >> modeler from default to database generated. Yep, that's a good approach to PK generation in SQLServer. >> But when I executed my java >> code again, it was suppose to insert only 1 record in database but it = also >> inserted previous record which I got in exception.=20 When you inserted the second record into ObjectContext, the context was = already "dirty", i.e. contained changes from somewhere else. My guess is = you are reusing the same ObjectContext across multiple web requests. You = are probably using CayenneFilter to bootstrap Cayenne, which provides = session-scoped context by default. There are different ways to solve it. = Here is one - you can switch to request-scoped context (if you don't = need to preserve uncommitted changes across requests): # Create a custom module overriding RequestHandler=20 # to use = org.apache.cayenne.configuration.web.StatelessContextRequestHandler package com.foo; ... public class MyModule implements Module { @Override public void configure(Binder binder) { binder.bind(RequestHandler.class) .to(StatelessContextRequestHandler.class) .withoutScope(); } } # load it in CayenneFilter in web.xml: cayenne-project = org.apache.cayenne.configuration.web.CayenneFilter extra-modules com.foo.MyModule =20 Hope this helps. Andrus > On Sep 11, 2015, at 5:16 PM, Dipesh Jain wrote: >=20 > I have tried to implement @Aristedes Maniatis answer but still result = was > same. I want to know why and how apache cayenne is doing this. I do = not > want the solution because I already have the answer. >=20 > On Wed, Sep 9, 2015 at 9:41 PM, Dipesh Jain wrote: >=20 >> I have written a code in java 8 to insert data in sql server 2012 = using >> apache cayenne 3.1. While executing code I got an exception : >> **org.apache.cayenne.CayenneRuntimeException: [v.3.1 Sep 20 2014 = 14:24:57] >> Commit Exception Caused by: java.sql.SQLException: Could not find = stored >> procedure 'auto_pk_for_table'**. >>=20 >> I have solved the problem by changing my pk generation strategy in = cayenne >> modeler from default to database generated. But when I executed my = java >> code again, it was suppose to insert only 1 record in database but it = also >> inserted previous record which I got in exception. I have tried = creating >> the same scenario thrice but I got the same result. I also tried = restarting >> my web server and SQL server service after getting an exception, but = at the >> time of inserting new data exceptional record was also been inserted = at the >> same time. >> This Problem is also solved by inserting a rollback statement in my = catch >> block. >>=20 >> But I want to know that how and why is apache cayenne inserting >> exceptional data at the time of inserting new data. >>=20 >> This is my code. >> **Java :** >> PersonDao >>=20 >> try { >> Person person =3D new Person(); >> person.setFirstName("John"); >> person.setLastName("Cross"); >> ObjectContext context =3D BaseContext.getThreadObjectContext(); >> context.registerNewObject(person); >> context.commitChanges(); >> } catch (CayenneRuntimeException e) { >> context.rollbackChanges(); >> throw e; >> } >>=20 >> **XML files :** >> cayenne-test.xml >>=20 >> >> >> > value=3D"true"/> >> >> > = factory=3D"org.apache.cayenne.configuration.server.XMLPoolingDataSourceFac= tory" >> = schema-update-strategy=3D"org.apache.cayenne.access.dbsync.CreateIfNoSchem= aStrategy"> >> >> >> >> >> >> >> >> >> >>=20 >> TestDataMap.map.xml >>=20 >> >> > xmlns:xsi=3D"http://www.w3.org/2001/XMLSchema-instance" >> xsi:schemaLocation=3D"http://cayenne.apache.org/schema/3.0/modelMap >> http://cayenne.apache.org/schema/3.0/modelMap.xsd" >> project-version=3D"6"> >> >> >> > isGenerated=3D"true" isMandatory=3D"true" length=3D"10"/> >> >> >> >> > className=3D"com.org.ivcargo.platform.dto.PersonTemp" >> dbEntityName=3D"PersonTemp"> >> > db-attribute-path=3D"personId"/> >> > db-attribute-path=3D"firstname"/> >> > db-attribute-path=3D"lastname"/> >> >> >>=20 >> -- >> Thanks and Regards >> Deepesh Jain >>=20 >=20 >=20 >=20 > --=20 > Thanks and Regards > Deepesh Jain