You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by Mathieu Frenette <ma...@freeborders.com> on 2002/02/13 18:15:23 UTC

ID generation bug(?) in BasePeer's doInsert() and workaround

Greetings to all of you, amazing Turbine developers! :0)

IDBroker was not working for me, and as I stepped into BasePeer's doInsert()
method I understood why.  I patched BasePeer and now it works, but I'm still
unsure whether it's a bug or a misunderstanding on my side.

I apologize in advance if this message should have gone to the Turbine Users
list instead, but I think Torque's internals are more appropriate to the
Turbine Dev list (?).

Here's how it goes:

I create a new OM object and want to insert it into the corresponding table
while having Torque automatically generated the primary key (a NumberKey).
However, the peer's doInsert() calls buildCriteria() which creates a
criterion for ALL columns, including the primary key.  Then when BasePeer's
doInsert() is called with the Criteria, it needs to determine whether to use
ID generation, and for this purpose checks whether the PK is specified in
the criteria, which is the case.  So it skips ID generation, and NULL gets
inserted.

I didn't want to alter buildCriteria() because it's being used by many other
methods which may require the PK to be specified as criteria, so my only
option was to modify BasePeer.  Instead of only checking for presence of PK
in Criteria, I also check the value for nullity.  This way, a new object
with a null primary key will have its PK generated correctly.

Here's the original lines (BasePeer.java, line 775, rev. 1.22 from
jakarta-turbine-torque CVS):

        // pk will be null if there is no primary key defined for the table
        // we're inserting into.
        if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))
        {
            // Proceed with ID generation...
        }

And here's my modified version:

        // pk will be null if there is no primary key defined for the table
        // we're inserting into OR if primary key is null.
        if (pk != null && (!criteria.contains(pk.getFullyQualifiedName()) ||
criteria.get(pk.getFullyQualifiedName())==null))
        {
            // Proceed with ID generation...
        }

Please let me know if I'm doing things all wrong! ;-)

Cheers, and keep up the good work folks!

Best regards,
	Mathieu Frenette
	Software Architect
	Freeborders Canada Inc.


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