From: "Hristo Kosev" <[EMAIL PROTECTED]>
> It sounds perfect. Really. But what's Andrea going to
> see is that he has a new row inserted in DETAILS table
> with REQUEST column set to NULL(that's in case he has
> set the required to "false", in case he hasn't he'll
> get a SQLException). I tried it with MySQL and that's
> what happened. So... there's something wrong with
> these Peer classes.
>
> Hristo Kosev
Hristo,
I disagree. The behaviour I described is exactly what occurs on
my system using MySQL.
Here is the behaviour I see:
Log.debug("debug", "Before save()");
Log.debug("debug", "request.getId(): " + request..getId()); // Outputs "null"
Vector v = result.getDetailss(); // (note the double "s" die to the naming
convention used)
Iterator iter = v.iterator();
while (iter.hasNext()) {
Details item = (Details) iter.next();
Log.debug("debug", "item.getId(): " + item.getId()); // Outputs "null"
}
ue.save(); // The magic line
Log.debug("debug", "After save()");
Log.debug("debug", "request.getId(): " + request..getId()); // Outputs some value
- e.g. "12"
v = result.getDetailss();
iter = v.iterator();
while (iter.hasNext()) {
Details item = (Details) iter.next();
Log.debug("debug", "item.getId(): " + item.getId()); // Outputs the same value
as for request - e.g. "12"
}
I have altered the above to suit the example we are using here
so it may include one or more typos, but this is the representative
output from a similar parent child relationship in my application.
Did you set up the foreign key correctly in your schema?
Did you regenerate your database and om layer before trying it?
This definitely works. I have an application with around 30
tables with many similar relationships and I have never had
to worry about retrieving the parent id in this kind of situation.
Does anyone else want to confirm this?
Cheers,
Scott
>
> --- Scott Eade <[EMAIL PROTECTED]> wrote:
> > 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.
> >
> > ----- 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-)
>
> __________________________________________________
> Do You Yahoo!?
> Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger
> http://im.yahoo.com
>
> ---------------------------------------------------------------------
> 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]