If you set your schema up correctly you don't need the id.

Here is what you should have:

<table name="DETAILS">
...
    <foreign-key foreignTable="REQUEST">
      <reference local="REQUEST" foreign="ID"/>
    </foreign-key>
</table>

When torque generates the OM layer it will generate a method:

    Request.addDetails(Details)

[BTW: It is better to name your tables in the singular - i.e. DETAIL
rather than DETAILS.  I also find it better to have more meaningful 
column names - i.e. DETAIL_ID, REQUEST_ID, etc.  and to use
the full key name where appropriate - i.e. DETAILS would use
REQUEST_ID rather than REQUEST.  But these may just be my
preferences.]

So this means that you don't need the id.  You go like this:

    Request r = new Request();
    r.setSrc(3);
    ...
    Details d = new Details();
    d.setCode(5);
    ...

... and here is the magic line:

    r.addDetails(d);

Now when it comes to saving the records all you need to do is:

    r.save();

... and this will save all of the Details records you have added to the 
Request as well as the Request itself.  I am pretty sure it also wraps 
them in the appropriate transaction logic (which is I guess ignored for
MySQL - can anyone tell me it works when using one of the table
drivers that does support transactions - i.e. BerkeleyDB, InnoDB
Gemini, etc.)

Whilst the code generated by torque doesn't do everything, it does
do a hell of a lot.  Set your schema up correctly and then take the 
time to examine the methods generated in the Base~ and Base~Peer
classes - there are plenty of goodies to make your life easier.

HTH

Scott

----- Original Message ----- 
From: "Andrea Papotti" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, August 31, 2001 9:00 PM
Subject: autoincrement and key retriving


> I have two tables like:
> 
> <table name="REQUEST">
>     <!-- substitution requests (header) -->
>     <column name="ID"          type="INTEGER"     required="true"
> autoIncrement="true" primaryKey="true"/>
>     <column name="SRC"       type="INTEGER"     required="true"/>
>     <column name="DST"       type="INTEGER"     required="true"/>
>     <column name="DATE"    size="8"  type="VARCHAR"     required="true"/>
>     <column name="NOTE"    size="80" type="VARCHAR"     required="true"/>
>  </table>
> 
>   <table name="DETAILS">
>     <!-- substitution requests (detail) -->
>     <column name="ID"                 type="INTEGER"     required="true"
> autoIncrement="true" primaryKey="true"/>
>     <column name="REQUEST"    type="INTEGER"     required="true"/>
>     <column name="CODE"           type="INTEGER"     required="true"/>
>     <column name="QTY"              type="INTEGER"     required="true"/>
>   </table>
> 
> which are related via REQUEST.ID = DETAILS.REQUEST
> 
> 
> I need to write 1 record in "REQUEST" fallowed by many records in "DETAILS",
> 
>         try
>         {
>             Richieste header = new Request();
>             header.setSrc( src_code );
>             header.setDst( dst_code);
>             header.setDatel( today() );
>             header.setNote( note );
>             header.save();
> 
>             header_id =  header.getId().getBigDecimal().intValue();
>         }
>         catch (Exception e)
>         {
>             context.put("error", "header: " + e.toString());
>             return;
>         }
> 
> 
> fallowed by code to write records in "DETAILS".
> 
> BUT
> 
> header_id = header.getId().getBigDecimal().intValue();
> 
> fails bacause header.getId() returns null.
> 
> the question is:
> 
> I must re-read the record just saved (with the immediatly previous
> statment), or there is a method to obtain the value of the primary-key just
> afer the .save() call?
> 
> Re-read the record could be a problem, because the only unique field is
> right the ID.
> 
> tia, Andrea  =8-)
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to