You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-user@db.apache.org by David Zhao <wz...@gmail.com> on 2006/10/20 00:24:28 UTC

How to check if row exists or retrieve last inserted id after insert when using Torque OM?

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,

David


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


Re: How to check if row exists or retrieve last inserted id after insert when using Torque OM?

Posted by Thomas Vandahl <tv...@apache.org>.
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