I now see a problem with the code below, it makes a new instance, which is
not what you want.
Looking at your code again I now think you actually don't have to do
anything at all!!!
This would be your code:
public void insertAnimal(final Animal newAnimal) throw AnimalException
{
try
{
Transaction tx = odmg.newTransaction();
tx.begin();
tx.lock(newAnimal, Transaction.WRITE);
tx.commit();
}
catch (Exception ex)
{
throw AnimalException.insertError("Something went wrong.");
}
}
Because you don't want to create a new instance, it shouldn't even be
necessary to do the cast. OJB should be able to determine the actual class
of newAnimal to be either a Cat or a Dog, without doing anything to the
input parameter at all.
Anyway, sorry if I send you in the wrong direction. However I still advise
you to look into the java.lang.reflect package and at the class
java.lang.Class. They offer nice ways of working with classes and objects if
you don't know the actual type ahead of time.
-Stijn
----- Original Message -----
From: "Stijn de Witt" <[EMAIL PROTECTED]>
To: "OJB Users List" <[EMAIL PROTECTED]>
Sent: Monday, June 21, 2004 10:44 AM
Subject: Re: Inserting through ODMG
> 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]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]