On Mon, 2004-06-21 at 10:53, Stijn de Witt wrote:
Yes... I remember I first tried to do it that way but I had problems,
that was why I turned to type-casting and creating new instances. This
was with RC5 and a month ago, now I am working with RC6 and everything
seems fine with your approach. THANKS! :)
> 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
> >
> >
--
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]