db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Vandahl ...@apache.org>
Subject Re: How to check if row exists or retrieve last inserted id after insert when using Torque OM?
Date Fri, 20 Oct 2006 07:45:50 GMT
David Zhao wrote:
> Hi there,
> 
> How do I either check if a certain record exists in the table using Torque
> OM class, or insert a new record, and retrieve the last_inserted_id?
> Thanks,

1.) There are two ways. Either you set up a Criteria object containing
the primary key (see the XXXPeer.buildCriteria(pk) method), run a
XXXPeer.doSelect(criteria) and check if the returned list is empty. Or
you use XXXPeer.retrieveByPK(pk) and catch the NoRowsException.

2.) If an object isNew(), it will be inserted into the database on
save(). Then the primary key fields are updated with the value of the id
just created. So you can check for this value using getPrimaryKey()
after save. If you do  this in a transaction, you can use this value
immediately to set it in dependent objects.

Bye, Thomas.


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


Mime
View raw message