Hi,
I would like to make a generic method for inserting objects from 2
classes, lets say class Cat and class Dog (both coming from Animal),
instead of using to different insert methods (this would be insertCat()
and insertDog().
My approach is the following:
public void insertAnimal(final Animal newAnimal) throw AnimalException
{
/*
Implementation odmg = is the implementation;
Database db = is the database already open;
*/
try
{
Transaction tx = odmg.newTransaction();
String classname=newItem.getClass().toString();
tx.begin();
if(classname.compareTo("Dog")==0)
{
Dog obj=(Dog)newItem;
tx.lock(obj, Transaction.WRITE);
}
else if(classname.compareTo("Cat")==0)
{
Cat obj=(Cat)newItem;
tx.lock(obj, Transaction.WRITE);
}
tx.commit();
}
catch (Exception ex)
{
throw AnimalException.insertError("Something went wrong.");
}
}
Is there a better way to do this?
The problems appear when instead of 2 classes I have 23...
--
Saludos,
Martin I. Levi
Centre Tecnol�gic de Transferenci�ncia de Calor
Universitat Polit�cnica de Catalunya
www.cttc.upc.edu
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]