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: Inheritance - How to know if it's a super or subclass
PostPosted: Mon Mar 14, 2005 8:19 pm 
Newbie

Joined: Tue Mar 01, 2005 5:00 pm
Posts: 11
Hello,

I'm a new hibernate user. Every day I understand something new.

My question now is:

I'have implemented a superclass (for example Cat) and a subclass (for example DomesticCat that extends Cat).
Everything runs perfectly, but...
If I would like yo load a Cat but I don't know if it is Cat o DomesticCat, what can I do?

If I load it as Cat, have I something to do to know then if it's a Cat or DomesticCat? Exists something like cat.isInstanceOf(DomesticCat()).

Than you!!

Javier.


Hibernate version: 3


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 4:09 pm 
Beginner
Beginner

Joined: Fri May 21, 2004 12:21 pm
Posts: 22
Location: Sacramento CA, USA
The whole purpose of doing the inheritance structure is that, you need not have to know what class you are retrieving. That is you can query it in polymorphic way. If you want a sub class you can ask for it. cat.isInstanceOf is not a good way of using inheritance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 4:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Agreed. But if you really have to, use Hibernate.getClass().

But it is much, much better OO practice to rely on polymorphism. Use of instanceof is a code smell.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 4:19 pm 
Beginner
Beginner

Joined: Fri May 21, 2004 12:21 pm
Posts: 22
Location: Sacramento CA, USA
I agree with Gavin's view on "instanceof". If you have to use it many times in your code, probably there is some thing wrong in the way you modeled your class.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 6:58 pm 
Newbie

Joined: Tue Mar 01, 2005 5:00 pm
Posts: 11
Thank you very much all.

Your answers have been very util for me.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 20, 2005 9:30 pm 
Newbie

Joined: Sun Mar 20, 2005 9:03 pm
Posts: 3
Could I obtain a recommendation to the following problem:

DomesticCat has a vaccinationDate attribute, WildCat does not, it doesn't make sense for him to have one. I need to list all the Cats treated by a VetinaryClinic, and print their potential vaccinationDates. Hence I'd iterate though VetinaryClinic.getAllTreatedCats(), check the type, and if it's a DomesticCat, load the appropriate proxy and fetch the vaccinationDate.

Am I incorrect in the assumption that both DomesticCat and WildCat should be mapped to the same table, but only the DomesticCat exposes vacinatedDate attribute? Should I use something along the lines of:

Cat derivedTypeProxy = (Cat) session.load(Hibernate.getClass(cat), cat.getId());
if (derivedTypeProxy instanceof DomesticCat) {
// can fetch the vaccinationDate
}

Is this the recommended way, am I missing something?


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.