Tim,
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 <selectKey> 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
<[EMAIL PROTECTED]> 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
<[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] Blog: http://cafe.jenkster.com/ Wiki: http://wiki.jenkster.com/