开发者

Mapping Map<String,Foo> in Hibernate

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? O开发者_开发技巧f otherwise defined: How can I map a one-to-many in a Map?

Thanks, Bart


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜