You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Bilge Erkan <bi...@polar.biz.tr> on 2004/02/23 02:15:32 UTC

Autoincrement field

Hello,

I want to use an autoincrement field which is at the same time a primary
key in a table in MySQL database. I think I have two choices for this in
  OJB.
1) I will either set the related field descriptor in repository with
autoincrement true and set the field in the database table as an integer
field,
2) or set the field in the database table as an autoincrement
integer field and not use the autoincrement attribute in the field
descriptor.

Which way is better?

If I prefer the second way, I have a trouble like this:
I can insert a new record. I follow the sql logs from the spy which are
as follows:

1077495828751|2|0|statement|SELECT SURNAME,USERNAME,NAME,PASSWD,ID FROM
USERS WHERE ID = ? |SELECT SURNAME,USERNAME,NAME,PASSWD,ID FROM USERS
WHERE ID = '0'
1077495828756|1|0|statement|INSERT INTO USERS
(ID,USERNAME,NAME,SURNAME,PASSWD) VALUES (?,?,?,?,?) |INSERT INTO USERS
(ID,USERNAME,NAME,SURNAME,PASSWD) VALUES ('0','JJJ','','','JJJ')
1077495828757|0|0|commit||

I think that OJB first checks the db if there is a record with the same
primary key, then executes an insert statement. The value of the id
field is 0 since mysql will give an autoincremented value to it.

When I try to insert a second, record the sql log is as follows:

1077495624868|3|0|statement|UPDATE USERS SET
USERNAME=?,NAME=?,SURNAME=?,PASSWD=? WHERE ID = ? |UPDATE USERS SET
USERNAME='EEE',NAME='',SURNAME='',PASSWD='EEE' WHERE ID = '0'
1077495624873|2|0|commit||

Here OJB executes only an update statement, and since there is no record
with id = 0, this new record cannot persist. The trouble is that the id
of this new object is again 0.

Is there a way to overcome this problem ?
My temporary solution is to delete the object with id = 0 from the
object model with "db.deletePersistent(user)" statement. Nothing is
deleted from the database table since there is no persistent record with
id = 0, but only the object from the object model is deleted. In this
way, even though the next created object has id = 0, OJB executes an
insert statement. I think this is not a good solution. Is there a better
solution?

Is there a way to retrieve the value of the id field given by the
database server, to the object model? If this value can be retrieved,
the problem that I mentioned above for the next object would be eliminated.

Thanks,
Bilge





---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Autoincrement field

Posted by "Robert S. Sfeir" <ro...@codepuccino.com>.
Bilge Erkan wrote:

> Hello,
>
> I want to use an autoincrement field which is at the same time a primary
> key in a table in MySQL database. I think I have two choices for this in
>  OJB.
> 1) I will either set the related field descriptor in repository with
> autoincrement true and set the field in the database table as an integer
> field,
> 2) or set the field in the database table as an autoincrement
> integer field and not use the autoincrement attribute in the field
> descriptor.
>
> Which way is better?

#1, unless you're only going to use one DB ever and never change to 
another DB.  If you plan on making things work with multiple DBs, than 
you pretty much have to use #1 because you can't guarantee a DB will 
have an autoincrement value, and you don't know how you can get the 
value out of autoincrement, since each DB will be different.

R

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org