You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by "Martin I. Levi" <ml...@labtie.mmt.upc.es> on 2004/06/21 09:58:16 UTC

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: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Inserting through ODMG (corrections)

Posted by "Martin I. Levi" <ml...@labtie.mmt.upc.es>.
On Mon, 2004-06-21 at 09:58, Martin I. Levi wrote:

Where it said "to different" it should say two different 
and where it says "newItem" it should say "newAnimal", sorry!


> 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: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Inserting through ODMG

Posted by "Martin I. Levi" <ml...@labtie.mmt.upc.es>.
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" <St...@bergland-it.nl>
> To: "OJB Users List" <oj...@db.apache.org>
> 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: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Inserting through ODMG

Posted by Stijn de Witt <St...@bergland-it.nl>.
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" <St...@bergland-it.nl>
To: "OJB Users List" <oj...@db.apache.org>
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" <ml...@labtie.mmt.upc.es>
> To: "OJB Users List" <oj...@db.apache.org>
> 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: ojb-user-unsubscribe@db.apache.org
> > For additional commands, e-mail: ojb-user-help@db.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Inserting through ODMG

Posted by Stijn de Witt <St...@bergland-it.nl>.
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" <ml...@labtie.mmt.upc.es>
To: "OJB Users List" <oj...@db.apache.org>
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: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org