This is what I found.
Whenever I tried inserting a new record in a table with the primary key set
specifically in code, the inserts would happen but the primary key I
provided would be lost. Instead, the auto generated number would be used to
fill in the primary key for that record.
I did some tests and found that the Criteria was not being constructed
properly and missed the primary key column info.
After digging in Torque's code I modified the buildCriteria method in the
Peer.vm template file to look like this.

/** Build a Criteria object from the data object for this peer */
    public static Criteria buildCriteria( $table.JavaName obj )
    {
        Criteria criteria = new Criteria(DATABASE_NAME);
        #foreach ($col in $table.Columns)
            #set ( $cfc=$col.JavaName )
            #set ( $cup=$col.Name.toUpperCase() )
            #if ($col.isPrimaryKey() && !$table.IdMethod.equals("none"))
    // Modified the original template to include the second condition in
the if statement below
        if (!obj.isNew() || (obj.get${cfc}() != null))
           #end
            criteria.add($cup, obj.get${cfc}());
        #end
        return criteria;
    }


The second condition in the if check above ensures that the primary key
column is set in the resulting Criteria object and the record inserted uses
this key instead of any auto-generated values.
Now the inserts work fine.

-Tanay




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

Reply via email to