-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Which child class should be cast ?
PostPosted: Sun Jan 22, 2006 10:51 pm 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1

Name and version of the database you are using:MySql 5.0

Hi guys,

I got 3 classes below:

Code:
public abstract class Animal{
   private Long id;
   private String name;
}

public class Dog extends Animal{
        private boolean bark;
        private String typeOfDogFood;
}

public class Cat extends Animal{
        private boolean miau;
        private String typeOfCatFood;
}


if I do a query like below:

Code:
public List findAnimal(Long id) {
      return getHibernateTemplate().find(
            "from Animal ani where ani.id = ?",
            new Object[] { id});
}


and return a List, how do I know which class, Dog or Cat should I cast to when iterating over the list:

Code:
    for (Iterator it=clist.iterator(); it.hasNext(); ) {
        Object element = (Dog)it.next();
        Object element = (Cat)it.next();
    }


if I cast the wrong class, system will throw severe exception ! Pls help, Thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 3:35 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Change your code like this

for (Iterator it=clist.iterator(); it.hasNext(); ) {
Animal element = (Animal)it.next();
if(element instanceof Dog){
Dog dog = (Dog)element ;
}else{
Cat cat = (Cat)element ;
}
}

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 11:56 pm 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
Your code will work only if the return query if instance of Animal like the query below:
Code:
public Animal findAnimal(Long id) {
    Animal ani = (Animal )getHibernateTemplate().get(Animal .class, id);
    return ani;
}


Please help on the one returning a List, Thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 12:55 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
No, ranmath's suggestion will work, no matter what. Dog and Cat will both cast to Animal just fine.
Code:
for (Animal ani : findAnimal(id))
{
  if (ani instanceof Dog)
  {
    Dog dog = (Dog) ani;
  }
}
This will work just fine.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 1:33 am 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
Hi,

I really don't know why my case doesn't work. My query is like below:

Code:
public List findAnimal(List id) {
      String[] param = {"ani_ids"};
      return getHibernateTemplate().findByNamedQueryAndNamedParam(
            "from Animal ani where ani.id in (:ani_ids)",param,
            new Object[] { id});
}


Code:
List aniList = new ArrayList();
aniList.add(new Long(1000));
aniList.add(new Long(2000));
aniList.add(new Long(3000));

List <Animal> list = animalManager.findAnimal(aniList)
for(Animal element : list){
   if(element instanceof Dog)
      log.info("Dog");
   else if(element instanceof Cat)
      log.info("Dog");
   else
      log.info("Animal ");   
}


and the log always print out Animal. Please help, Thanks !

regards,
Mark


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 4:25 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you're always getting objects of type Animal and you're expecting other types, then your mapping must be wrong. Can you post that?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.