Try using reflection instead of comparing classnames as strings.
Don't do:
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);
}
But instead do:
Class argClass = Class.forName(classname);
Object animal = argClass.instance();
tx.lock(animal, Transaction.WRITE);
Good luck,
Stijn
----- Original Message -----
From: "Martin I. Levi" <[EMAIL PROTECTED]>
To: "OJB Users List" <[EMAIL PROTECTED]>
Sent: Monday, June 21, 2004 9:58 AM
Subject: Inserting through ODMG
> 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]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]