You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by "Pugh, Eric" <EP...@MuseumCompany.com> on 2001/12/07 13:51:26 UTC

RE: Copying and then saving a row into DB, how do I get the prima ry key

I read/searched around, and it seems that when you do the insert in the save
method of the BasePeer, you should get an object back with the primary key.
However, I get a null, which I think is because my idMethod is none.
Therefore after the insert, Torque has no idea what the primary key was,
because it is an identity column.  

I ended up doing a custom executeQuery: Select max(product_id) from
products.  However this does raise the issue that if two people do two
inserts at the same time, they might both get the same product id back!  

I will try the setNew.  What does that actually do (other then tell torque
that its needs to do an insert)?  Also, I think doing the copy causes the
setNew(true) to happen.

Eric

-----Original Message-----
From: Scott Eade [mailto:seade@backstagetech.com.au]
Sent: Thursday, December 06, 2001 5:53 PM
To: Turbine Users List
Subject: Re: Copying and then saving a row into DB, how do I get the
primary key


----- Original Message -----
From: "Pugh, Eric" <EP...@MuseumCompany.com>
> Hi all,
>
> I looked through the archives to find the answer, b/c I swear I saw the
> answer, but no luck...
>
> I am using MSSQL as my database, and all my tables are set with
> idMethod=none, using Turbine 2.1.
>
> I want to copy a product object with product id 2.   So I do:
> productB = productA.copy();

Try adding this:

    productB.setNew(true);

> productB.save();
>
> This saves my new row in the database, but my productB's primary key is
> null.  I used profiler and see the we are doing an insert, but there
doesn't
> be anything like return @@Identity or anything to pass back what my new
> primary key is.
>
> Is the only way of figuring what productB's primary key is to do a select
> max(product_id) on my products table?
>
> Eric

HTH.

Scott



--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Copying and then saving a row into DB, how do I get the prima ry key

Posted by Daniel Rall <dl...@finemaltcoding.com>.
"Pugh, Eric" <EP...@MuseumCompany.com> writes:

> However, I get a null, which I think is because my idMethod is none.
> Therefore after the insert, Torque has no idea what the primary key was,
> because it is an identity column.  

To confirm, that's the expected behavior.

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Copying and then saving a row into DB, how do I get the primary key

Posted by John McNally <jm...@collab.net>.
I think idbroker is a form of hilo.  It grabs a range of ids in one call
to the db and keeps a local count until that range is used up.  What
feature of hilo do you desire that is not a feature of idbroker.  There
is a guid class in cvs as well, though it does not implement the
IdGenerator interface.  I would say the work to add it is minimal in
difficulty, but may touch quite a few files.

john mcnally

Ian Lim wrote:
> 
> Hi
> 
> Is it quite difficult to implement something like idMethod="guid" or
> idMethod="hilo"
> Where a globally unique id can be use for the primary key generation?
> 
> Regards
> ==========
> Ian Lim
> email: mallim_sg@yahoo.com.sg
> homepage: http://www.webappcabaret.com/mallim
> 
> ----- Original Message -----
> From: "John McNally" <jm...@collab.net>
> To: "Turbine Users List" <tu...@jakarta.apache.org>
> Sent: Tuesday, December 11, 2001 12:30 AM
> Subject: Re: Copying and then saving a row into DB, how do I get the primary
> key
> 
> > Torque expects that the code will have supplied the pk's prior to the
> > insert when using idMethod="none".  If the db is generating the id, you
> > should use autoincrement (i think that is most appropriate for an
> > identity), or sequence, or native for the latest cvs.
> >
> > "Pugh, Eric" wrote:
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Copying and then saving a row into DB, how do I get the primary key

Posted by Ian Lim <ma...@ematic.com>.
Hi

Is it quite difficult to implement something like idMethod="guid" or
idMethod="hilo"
Where a globally unique id can be use for the primary key generation?

Regards
==========
Ian Lim
email: mallim_sg@yahoo.com.sg
homepage: http://www.webappcabaret.com/mallim

----- Original Message -----
From: "John McNally" <jm...@collab.net>
To: "Turbine Users List" <tu...@jakarta.apache.org>
Sent: Tuesday, December 11, 2001 12:30 AM
Subject: Re: Copying and then saving a row into DB, how do I get the primary
key


> Torque expects that the code will have supplied the pk's prior to the
> insert when using idMethod="none".  If the db is generating the id, you
> should use autoincrement (i think that is most appropriate for an
> identity), or sequence, or native for the latest cvs.
>
> "Pugh, Eric" wrote:



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Copying and then saving a row into DB, how do I get the primary key

Posted by John McNally <jm...@collab.net>.
Torque expects that the code will have supplied the pk's prior to the
insert when using idMethod="none".  If the db is generating the id, you
should use autoincrement (i think that is most appropriate for an
identity), or sequence, or native for the latest cvs.

"Pugh, Eric" wrote:
> 
> I read/searched around, and it seems that when you do the insert in the save
> method of the BasePeer, you should get an object back with the primary key.
> However, I get a null, which I think is because my idMethod is none.
> Therefore after the insert, Torque has no idea what the primary key was,
> because it is an identity column.
> 
> I ended up doing a custom executeQuery: Select max(product_id) from
> products.  However this does raise the issue that if two people do two
> inserts at the same time, they might both get the same product id back!
> 
> I will try the setNew.  What does that actually do (other then tell torque
> that its needs to do an insert)?  Also, I think doing the copy causes the
> setNew(true) to happen.
> 
> Eric
> 
> -----Original Message-----
> From: Scott Eade [mailto:seade@backstagetech.com.au]
> Sent: Thursday, December 06, 2001 5:53 PM
> To: Turbine Users List
> Subject: Re: Copying and then saving a row into DB, how do I get the
> primary key
> 
> ----- Original Message -----
> From: "Pugh, Eric" <EP...@MuseumCompany.com>
> > Hi all,
> >
> > I looked through the archives to find the answer, b/c I swear I saw the
> > answer, but no luck...
> >
> > I am using MSSQL as my database, and all my tables are set with
> > idMethod=none, using Turbine 2.1.
> >
> > I want to copy a product object with product id 2.   So I do:
> > productB = productA.copy();
> 
> Try adding this:
> 
>     productB.setNew(true);
> 
> > productB.save();
> >
> > This saves my new row in the database, but my productB's primary key is
> > null.  I used profiler and see the we are doing an insert, but there
> doesn't
> > be anything like return @@Identity or anything to pass back what my new
> > primary key is.
> >
> > Is the only way of figuring what productB's primary key is to do a select
> > max(product_id) on my products table?
> >
> > Eric
> 
> HTH.
> 
> Scott
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Copying and then saving a row into DB, how do I get the primary key

Posted by Scott Eade <se...@backstagetech.com.au>.
From: "Pugh, Eric" <EP...@MuseumCompany.com>
> I read/searched around, and it seems that when you do the insert in the
save
> method of the BasePeer, you should get an object back with the primary
key.
> However, I get a null, which I think is because my idMethod is none.
> Therefore after the insert, Torque has no idea what the primary key was,
> because it is an identity column.

I totally missed that you said your idMethod was "none".  With "none" how
would you expect an id to be generated?  How does the id get assigned to
the original object?

Assuming you are using tdk2.1 why not set idMethod to "autoincrement"
and let the database allocate the ids for you.  I much prefer
"autoincrement"
over "idbroker" as it allows you to create records external to your
application.

>
> I ended up doing a custom executeQuery: Select max(product_id) from
> products.  However this does raise the issue that if two people do two
> inserts at the same time, they might both get the same product id back!

I assume idBroker takes steps to prevent this.  "autoincrement" should
handle this also.

> I will try the setNew.  What does that actually do (other then tell torque
> that its needs to do an insert)?  Also, I think doing the copy causes the
> setNew(true) to happen.

Yes, it tells torque to do an insert.  Copy may well set the new flag.
Ultimately my comment was misdirected because I skipped over
your idMethod.

> Eric

Cheers,

Scott


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>