Return-Path: Mailing-List: contact ibatis-user-java-help@incubator.apache.org; run by ezmlm Delivered-To: mailing list ibatis-user-java@incubator.apache.org Received: (qmail 41265 invoked by uid 99); 23 Feb 2005 17:38:14 -0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=RCVD_BY_IP,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (hermes.apache.org: domain of brandon.goodin@gmail.com designates 64.233.170.197 as permitted sender) Received: from rproxy.gmail.com (HELO rproxy.gmail.com) (64.233.170.197) by apache.org (qpsmtpd/0.28) with ESMTP; Wed, 23 Feb 2005 09:38:12 -0800 Received: by rproxy.gmail.com with SMTP id c16so786179rne for ; Wed, 23 Feb 2005 09:38:01 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=eDBw6M8H9t9lZpNFgdnWh6X/mVM4bhGzlOSUIzBRk2lPdvSID0H1sTU78SVnx9I45MYIqc6GJbQKm5cQ27rSb/jF6+qMX5OUPqjcikDXvPgB3+fU+xB4f2HcuwzMq/xWLXVXLVTWpgDDkxELXtDHeWwupYQI7yfhe2eBDh6CD2I= Received: by 10.38.12.9 with SMTP id 9mr92983rnl; Wed, 23 Feb 2005 09:11:20 -0800 (PST) Received: by 10.38.74.53 with HTTP; Wed, 23 Feb 2005 09:11:19 -0800 (PST) Message-ID: <2fe5ef5b050223091174add5ec@mail.gmail.com> Date: Wed, 23 Feb 2005 10:11:19 -0700 From: Brandon Goodin Reply-To: Brandon Goodin To: ibatis-user-java@incubator.apache.org Subject: Re: Duplicate Key in Db In-Reply-To: <421CB7E0.7000706@yahoo.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <63394b52050223040043f3d360@mail.gmail.com> <2fe5ef5b050223073826588494@mail.gmail.com> <63394b520502230806135dbe29@mail.gmail.com> <421CB7E0.7000706@yahoo.co.uk> X-Virus-Checked: Checked "The module code is a primary key and must be a unique value, though it must match with a value stored in another system so can not be an auto-increment value." I don't think his situation would allow for that. Brandon > I'd want to design it so that the database assigns the primary keys. > It's the best way to do it IMHO. Any object to be persisted is checked > for a primary key. If it's null, you do an insert; if it's not null, > you want an update. The database assigns primary keys and can use a > sequence to guarantee uniqueness. Then you can use the > element in SqlMap* to assign the sequence number back into the object. > This way you avoid the whole problem. > > HTH, > Kris > > * See the dev guide for details. > > > Tim Christopher wrote: > > >Sorry, I should have mentioned that I was talking about an > >insertion... Currently it is possible for a user to create a new > >Module by specifying a module code, name, etc.. The module code is a > >primary key and must be a unique value, though it must match with a > >value stored in another system so can not be an auto-increment value. > > > >Is there any way to prevent a user from creating two modules with the > >same Id, other than doing a select statement first to see if it will > >break any database rules. Doing it that way would mean any change to > >the database structure such as the removal of a primary keep will > >require an update to my Java classes. > > > >I was hoping there is a way for iBATIS catch the error and return > >something to let the user know their attempted insertion was invalid > >and was not completed. > > > >Tim Christopher > > > > > >On Wed, 23 Feb 2005 08:38:38 -0700, Brandon Goodin > > wrote: > > > > > >>you should not specify your primary key in the update values of your > >>update statement. It should be part of your where criteria. > >> > >>BAD: > >>UPDATE SOMETABLE > >>(somePrimaryKey,columnA,columnB,columnC) > >>VALUES(#somePrimaryKey#,#'valueA#,#valueB#,#valueC#) > >>WHERE somePrimaryKey = 1 > >> > >>GOOD: > >>UPDATE SOMETABLE > >>(columnA,columnB,columnC) > >>VALUES(#valueA#,#valueB#,#valueC#) > >>WHERE somePrimaryKey = #somePrimaryKey# > >> > >>Brandon > >> > >>On Wed, 23 Feb 2005 12:00:34 +0000, Tim Christopher > >> wrote: > >> > >> > >>>Hi, > >>> > >>>Apologies is this question is trivial for the group but I'm very new > >>>to using iBATIS. > >>> > >>>I've managed to successfully integrate iBATIS into my Struts > >>>application, using a structure much like the sample JPetStore project > >>>- this acted as my tutorial. > >>> > >>>Can someone let me know if it is possible for iBATIS to deal with > >>>problems such as an update containing a primary key which is the same > >>>as on already in the table? > >>> > >>>I tested this out this morning on got a huge stack trace (summarised > >>>below), so I'm guessing it isn't done by default. > >>> > >>> > >>> > >>>Cause: com.borland.datastore.driver.SqlState: Runtime Error: [line 1, > >>>col 6] Duplicate key value for $UNIQUE$1 sort order in "MODULE". > >>><<<< > >>> > >>>Any advice or even just a link would be much appreciated. > >>> > >>>Regards, > >>> > >>>Tim Christopher > >>> > >>> > >>> > > > > > > > > -- > Kris Jenkins > Email: kris@jenkster.com > Blog: http://cafe.jenkster.com/ > Wiki: http://wiki.jenkster.com/ > >