You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by "Weaver, Scott" <Sw...@rippe.com> on 2002/01/10 14:59:18 UTC

RE: Can't retrieve the primary key after an insert of a new recor d!

Are you using "native" or "idBroker" for your id's?

If you are using "native", you will not be able to access the primary key
after an insert.  "native" uses the RDBMS' internal ability to generate the
id, which consequently happens during/after the actual insert into the db,
and at this point it is out of Torque's control.  For the key to be
available to a Torque OM object, Torque would have to query the db after the
insert transaction was completed, which it does not.

However, you could add this functionality by overriding the save() method in
your OM class query the db for it's key.  

On the other hand, if you use "idBroker", which uses Torque's api to
generate id's, you will have access to the primary key because the key is
generated and stored in the OM class before the insert into the db not
during/after as is with the "native" id method.

Scott

-----Original Message-----
From: Fabio Daprile [mailto:fabio.daprile@wuerth.it]
Sent: Thursday, January 10, 2002 4:32 AM
To: turbine-user@jakarta.apache.org
Subject: Can't retrieve the primary key after an insert of a new record!


I have the following code... after the save (which is successful) I cannot
get back the key (which I know was made and used because I see it in the DB)
the same results if the primary key method is idbroker or auto_increment in
the Map file... I am using mysql..


here is my code;

        _ticket = new Tickets();
        _ticket.setEffectiveid("1");
        _ticket.setProjectid(String.valueOf(_projectId));
        _ticket.setQueueid(_queue.getId().toString());
        _ticket.setOwnerid(_user.getId().toString());
        _ticket.setSubject(_subject);
        _ticket.setDescription(_description.getBytes());
        _ticket.setArea(_area);
        _ticket.setHowtoreproduce(_howToReproduce.getBytes());
        _ticket.setPriority(_priority);
        _ticket.setRaisedrefversion(_raisedVersion);
        _ticket.setFixedrefversion("");
        _ticket.setStatus("new");
        _ticket.setTimeestimated(0);
        _ticket.setTimeworked(0);
        _ticket.setStartedtimestamp(new Date(0));
        _ticket.setDuetimestamp(new Date(0));
        _ticket.setResolvedtimestamp(new Date(0));
        _ticket.setLastupdatedbyid(_user.getId().toString());
        _ticket.setLastupdatetimestamp(Calendar.getInstance().getTime());
        _ticket.setCreatorid(_user.getId().toString());
        _ticket.setCreatedtimestamp(Calendar.getInstance().getTime());
        _ticket.setDisabled(0);


        _ticket.setNew(true);

        _ticket.save();

        System.out.println(_ticket.getPrimaryKeyAsInt());     <always null>
or    System.out.println(_ticket.getId().toString());               
<always null>

Can you help me?

-- 


Würth Phoenix Srl
Via Kravogl 4, I-39100 Bolzano
Tel: +39 0471/564111
Fax: +39 0471/564122

mailto:fabio.daprile@wuerth-phoenix.com
http://www.wuerth-phoenix.com



--
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: Can't retrieve the primary key after an insert of a new record!

Posted by John McNally <jm...@collab.net>.
"Weaver, Scott" wrote:
> 
> Are you using "native" or "idBroker" for your id's?
> 
> If you are using "native", you will not be able to access the primary key
> after an insert.  "native" uses the RDBMS' internal ability to generate the
> id, which consequently happens during/after the actual insert into the db,
> and at this point it is out of Torque's control.  For the key to be
> available to a Torque OM object, Torque would have to query the db after the
> insert transaction was completed, which it does not.
> 

Scott Eade said this already but I would like to make sure the point is
taken.  The above paragraph is wrong.  You can use "native" and
"idbroker" interchangeably.  The id should still be available after the
insert.  Perhaps "native" is not usable in the tdk?  Try
"autoincrement".

john mcnally

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


Re: Can't retrieve the primary key after an insert of a new record!

Posted by Scott Eade <se...@backstagetech.com.au>.
From: "Weaver, Scott" <Sw...@rippe.com>
> Are you using "native" or "idBroker" for your id's?

That should be "idbroker" with a lowercase "b".

> If you are using "native", you will not be able to access the primary key
> after an insert.  "native" uses the RDBMS' internal ability to generate
the
> id, which consequently happens during/after the actual insert into the db,
> and at this point it is out of Torque's control.  For the key to be
> available to a Torque OM object, Torque would have to query the db after
the
> insert transaction was completed, which it does not.

What you are saying appears to make sense but I do
recall being able to retrieve an id immediately after
an insert when using MySQL and autoincrement.
I also recall other users having this same problem -
check out:
http://marc.theaimsgroup.com/?l=turbine-user&m=100434715720285&w=2
I bet the problem the user had in the archived message was
the "I" in "autoIncrement".

> However, you could add this functionality by overriding the save() method
in
> your OM class query the db for it's key.

Unnecessary

> On the other hand, if you use "idBroker", which uses Torque's api to
> generate id's, you will have access to the primary key because the key is
> generated and stored in the OM class before the insert into the db not
> during/after as is with the "native" id method.

Native methods are good in that you can use other
tools to insert data and have consistent ids generated.
On the other hand they are not usually the best keys to
use (high-low ids would be better).

>> I have the following code... after the save (which is successful) I
cannot
>> get back the key (which I know was made and used because I see it in the
DB)
>> the same results if the primary key method is idbroker or auto_increment
in
>> the Map file... I am using mysql..

That should be "autoincrement" without the "_". No
doubt this is your problem.

It is quite easy to catch these problems by validating
your schema file.

Cheers,

A different Scott



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