-->
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.  [ 5 posts ] 
Author Message
 Post subject: Help with mapping using discriminator
PostPosted: Thu Nov 20, 2003 2:57 pm 
Newbie

Joined: Thu Oct 23, 2003 1:43 pm
Posts: 10
Location: Calgary, Alberta Canada
Damn, I'm confused.

I have an interface, Animal, and two classes, Cat and Dog that implement the interface. Cat and Dog each have their own table.

I have another class, FoundReport, that contains an instance of Animal which could be assigned either a Cat or Dog. And I can't figure out how to map all of this! I want my FoundReport table to have an integer animal id and an indicator which indicates which type of animal the id refers to. So when selecting, I need Hibernate to refer to the indicator column to determine which table, Cat or Dog, to join to on the value in the animal id column.

I think I need a discriminator property in FoundReport that would have values C or D, and then the Cat and Dog class mappings include the discriminator-value attribute to match. But how do I map the Animal property in FoundReport? Somehow, I have to tie the animal property and discriminator property together, and I can't figure out how to do that. I've read the Hibernate Ref multiple times, I'm missing something because I can't figure out how to map my scenario.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 3:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Hi,

this may probably help you:
http://forum.hibernate.org/viewtopic.php?t=925642

Either map your interface as a class and the concrete classes as <subclass> or use a <any> mapping and map Cat and Dog seperately as <class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 3:25 pm 
Newbie

Joined: Thu Oct 23, 2003 1:43 pm
Posts: 10
Location: Calgary, Alberta Canada
Thanks for the reference to that post, it is a scenario very much like mine.

I had examined subclass and joined-subclass briefly but the documentation says 'polymorphic persistence requires the declaration of each subclass of the root persistent class'. I dismissed these then because my root class isn't persistent.

But, if each of the classes has its own table, would this not be a joined-subclass mapping then? The post you referred me to seems to have the same scenario as mine, the subclasses each have their own table, but the suggestion there was also to use subclass. Is subclass really the right thing to use here? Or should it be joined-subclass as the docs seem to suggest?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 6:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you want each class as its own table, you cant use <joined-subclass> here, because it requires a table where the superclass is mapped to - additional properties of the subclasses would then be mapped to additional tables - which makes not much sense in the case of a common interface, because there are no common properties to map to the superclass table.

Using a <subclass> mapping would map both Cat and Dog to the same table, which would then include a discriminator column which specifies which type the row represents.

So the thing to do if you really need each class as its own table would be to map both cat and dog as normal <class> mappings, and use an <any> element for mapping the association in FoundReport. This would need two columns in the table for FoundReport, where one contains the ID of the associated animal, and the second the type of the associated element.

Note however that <any> is not that nice, because you can no longer specify a foreign key constraint in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 9:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
polymorphic persistence requires the declaration of each subclass of the root persistent class


yeah, this is not quite true now .... by combining queries with "implicit polymorphism" and <any> mappings, Hibernate supports polymorphism for table-per-concrete-class.

But the underlying relational model of an any mapping is not very nice, so we don't recommend this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.