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/14 14:48:54 UTC

Problem with IDBroker related to buildCriteria()

When I create a new OM object with a non-specified (null) PK and save it to
the DB, the IDBroker doesn't get invoked, even if my IDBroker and ID_TABLE
are correctly configured.  I think the problem comes for buildCriteria() or
the way it is used.

For example, the following:

	MyObj obj = new MyObj(null, "John", "Smith");
	obj.save();

Would result in this row being inserted:

	null, "John", "Smith"

I traced into the Peer code and understood why, but I can't see how to get
it to work without modifying Torque's source code.  Here are my findings:

The object's save() method calls the Peer's doInsert(), which in turn calls
buildCriteria().  buildCriteria() creates a criterion for *every* column of
the table, *including* the PK, even if this PK is null!

However, since my new OM object doesn't have a PK (obviously it is null at
this point in time), a criterion for "PK=null" is added to Criteria.  Then,
finally, when Torque needs to determine whether or not to use the IDBroker,
it checks to see if the PK is specified in the Criteria, which is the case
(but it doesn't check to see whether the *value* of the criteria is null,
only if it is present).  So it decides not to use the IDBroker.

I simply cannot understand how the IDBroker can be used, other than by using
it manually.  The automatic behavior could never be called, at least not
when calling .save() or .doInsert() for new OM objects, which always have
null PKs.

In order to get it to work temporarily, I had to modify BasePeer.java near
line 775 in the following way:

BEFORE:
if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))
{
	// Proceed with ID generation
}

AFTER:
if (pk != null &&
     (!criteria.containsKey(pk.getFullyQualifiedName()) ||
       criteria.getValue(pk.getFullyQualifiedName())==null))
{
	// Proceed with ID generation
}

I couldn't find any documentation regarding ID generation, so any help would
be greatly appreciated...

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>


Re: Problem with IDBroker related to buildCriteria()

Posted by Scott Eade <se...@backstagetech.com.au>.
> From: John McNally <jm...@collab.net>
> I think your schema must have an error, if you are not seeing code like
> this.  Are you validating?
> 
> john mcnally

This is good advice.  IMHO validating your schema is the first thing to do
when torque isn't behaving as expected.  I've seen this pick up problems
time and time again.

Scott


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


Re: Problem with IDBroker related to buildCriteria()

Posted by John McNally <jm...@collab.net>.
There is code in buildCriteria to not add the pk column, if the object
is new.  
Here some code generated for a table which uses the idbroker to set the
issue_id.


/** Build a Criteria object from the data object for this peer */
public static Criteria buildCriteria( Issue obj )
{
Criteria criteria = new Criteria(DATABASE_NAME);
if ( !obj.isNew() )
    criteria.add( ISSUE_ID, obj.getIssueId() );
criteria.add( ID_PREFIX, obj.getIdPrefix() );
criteria.add( ID_COUNT, obj.getIdCount() );
criteria.add( ID_DOMAIN, obj.getIdDomain() );
criteria.add( TYPE_ID, obj.getTypeId() );
criteria.add( MODULE_ID, obj.getModuleId() );
criteria.add( DELETED, obj.getDeleted() );
return criteria;
}

I think your schema must have an error, if you are not seeing code like
this.  Are you validating?

john mcnally
    
Mathieu Frenette wrote:
> 
> When I create a new OM object with a non-specified (null) PK and save it to
> the DB, the IDBroker doesn't get invoked, even if my IDBroker and ID_TABLE
> are correctly configured.  I think the problem comes for buildCriteria() or
> the way it is used.
> 
> For example, the following:
> 
>         MyObj obj = new MyObj(null, "John", "Smith");
>         obj.save();
> 
> Would result in this row being inserted:
> 
>         null, "John", "Smith"
> 
> I traced into the Peer code and understood why, but I can't see how to get
> it to work without modifying Torque's source code.  Here are my findings:
> 
> The object's save() method calls the Peer's doInsert(), which in turn calls
> buildCriteria().  buildCriteria() creates a criterion for *every* column of
> the table, *including* the PK, even if this PK is null!
> 
> However, since my new OM object doesn't have a PK (obviously it is null at
> this point in time), a criterion for "PK=null" is added to Criteria.  Then,
> finally, when Torque needs to determine whether or not to use the IDBroker,
> it checks to see if the PK is specified in the Criteria, which is the case
> (but it doesn't check to see whether the *value* of the criteria is null,
> only if it is present).  So it decides not to use the IDBroker.
> 
> I simply cannot understand how the IDBroker can be used, other than by using
> it manually.  The automatic behavior could never be called, at least not
> when calling .save() or .doInsert() for new OM objects, which always have
> null PKs.
> 
> In order to get it to work temporarily, I had to modify BasePeer.java near
> line 775 in the following way:
> 
> BEFORE:
> if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))
> {
>         // Proceed with ID generation
> }
> 
> AFTER:
> if (pk != null &&
>      (!criteria.containsKey(pk.getFullyQualifiedName()) ||
>        criteria.getValue(pk.getFullyQualifiedName())==null))
> {
>         // Proceed with ID generation
> }
> 
> I couldn't find any documentation regarding ID generation, so any help would
> be greatly appreciated...
> 
> 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>

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