You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-user@db.apache.org by Alexander Rau <ra...@gmx.de> on 2003/03/01 19:45:55 UTC

Re: obtaining native primary keys problem

>>Depends on ... I think it does not work with any DBxxxAdapter implementing
>>this:
>>
>>public String getIDMethodType()
>>    {
>>        return NO_ID_METHOD;
>>    }
>>
> 
> 
> sorry . rest of the message was lost (a lot !) gonna reply again in a few
> hours from at home)
> 

Ok. Once again.

DBDB2NET Adapter uses NO_ID_METHOD as IdMethodType. I think several 
other database adapters do so as well.

This implies that there will be no IDGenererator instance created ) as 
beside the normal IDBroker only AutoIncrementIDGenerator and 
SequenceIdGernerator exist.

Here's the code snippet:

public class IDGeneratorFactory
{ ....

     public static IdGenerator create(DB dbAdapter)
     {
         String idMethod = dbAdapter.getIDMethodType();
         if (IDMethod.AUTO_INCREMENT.equals(idMethod)) {
		return new AutoIncrementIdGenerator(dbAdapter);
         }
         else if (IDMethod.SEQUENCE.equals(idMethod)) {
		return new SequenceIdGenerator(dbAdapter);
         }
         else { return null; }     // DBDB2NET Adapter is handled here !
     }

....

}

Now the BasePeer.java class:

public static ObjectKey doInsert(Criteria criteria, Connection con) {

.....
// 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())) {

	if (keyGen == null) {
	throw new TorqueException("IdGenerator for table '" + tableName + "' is 
null");
	}


keygen is the earlier assign ((IdGenerator) null) from 
IdGeneratorFactory.create(DBDB2NET-Adapter). As you can see you'll get a 
TorqueException because the keygen is null. Actually it's somehow 
logically correct because the Database should generate our new primary 
keys. However the code does not catch that at all.

So the conclusion is: all DBAdapters using ID_METHOD_NONE will fail as 
soon as the standard IDBroker won't be used anymore.

Oracle (IDMethod = Sequence) and Mysql (IDMethod = AutoIncrement) users 
are lucky here as this ID retrieval method works.

This is a list of Databases which use ID_METHOD_NONE:
- Axion
- DB2App/Net
- Informix
- InstantDB
- Interbase
- ODBC
- Weblogic

Personally I'm going to try to patch the DBDB2NET adapter to use 
Sequences as DB2 supports this since V7.2 AFAIK.

Has anyone any idea how to replace the oracle sql string ?

public String getIDMethodSQL(Object sequenceName) {
     return ("select " + sequenceName + ".nextval from dual");
}

I think it's something like 'SELECT NEXTVAL FOR sequencename FROM 
tablename WHERE entry_receiving_new_pk'

dunno. trying to find that out this weekend.


Regards, Alex