views:

66

answers:

1

Hi all,

I'm trying to map my Hashmap in Hibernate. All examples I can find are simply like this:

class FooBar{
    Map<String,String> myStrings;
}

Which would simply map to

<map role="ages">
<key column="id"/>
<index column="name" type="string"/> 
<element column="age" type="string"/> 
</map>

However, I use a more object-oriented approach in my Java code. Kind of like the following:

class Bar{
    private Map<String, Foo> myFoos;
}

How would I go about mapping this? As the relationshop? Of otherwise defined: How can I map a one-to-many in a Map?

Thanks, Bart

+1  A: 

There are a few examples in the Hibernate reference manual chapter on Collection Mapping. You would want to do something like

<map name="foos">
    <key column="id"/>
    <index column="name" type="string"/> 
    <one-to-many class="Foo"/>
</map>

The difference is <one-to-many class="Foo"/> - this will map the relationship by using a foreign key column to the ID of the Foo table in the parent table (i.e. the object that has the Map of foos).

There are several more flavors and variations of how you can map this based on exactly the type of relationship you want, see the manual for more examples.

matt b