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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
