-->
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.  [ 4 posts ] 
Author Message
 Post subject: mapping map to polymorphic values
PostPosted: Mon Sep 22, 2003 12:20 pm 
Newbie

Joined: Mon Sep 22, 2003 12:00 pm
Posts: 3
I need advice on how to map a rather complex field in one of my persistent classes.

I have a java.util.Map that maps from a key to one of several possible objects. It's probably best to use a silly example:

Alice -> PERSON(wife)
Bob -> TEACHER(daughter's teacher)
Duke -> DOG

(a TEACHER extends PERSON, although I'm not always sure this is the case in real life.) In the actual business logic it's a bit more complex since the key also constraints the class of the target - it's more like:

LIGHT_BULBS -> LIGHTBULB_MANUFACTURER(...) or
-> LIGHTBULB_PURCHASER(...)

but never

! LIGHT_BULBS -> TONER_CARTRIDGE_MANUFACTURER

Maybe I'm missing something obvious, but I see this producing three tables:

FIE: the table that contains the bulk of the object, and contains a one-to-many 'map' relationship to FEE.

FEE: the table that contains the map, linking key and a one-to-one relationship to FOE, FUM, FAH

FOE: a table that contains PERSONs
FUM: a table that contains TEACHERs
FAH: a table that contains DOGs

Is this the simplest approach? Is it possible to store all targets in a single column - I want to avoid having one field for each possible type, for both business logic and practical reasons.

Finally, what are the Xdoclet tags for all of this?!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2003 2:12 pm 
Newbie

Joined: Mon Sep 22, 2003 12:00 pm
Posts: 3
Amazing how asking for help seems to shake something loose so you can find the answer yourself.

This seems to answer my problems, and with a lot less effort than I first feared:

Code:
@hibernate.map cascade="all"
@hibernate.collection-key column="key"
@hibernate.collection-index column="key" type="long"
@hibernate.collection-element column="value" type="serializable"


The main downside is that this approach is that a serializable object is opaque - the RHS of the map can't be used in Hibernate queries.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2003 3:24 pm 
Newbie

Joined: Mon Sep 22, 2003 12:00 pm
Posts: 3
I spoke too soon - this seems to produce a table that provides a general map, not one keyed to a specific instance....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2003 2:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
forget XDoclet for this.


Looks like you need a <many-to-any>


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