问题
I need a collection that stores entries as key-value pairs (so I can look up values by a key), but I need one that allows multiple values to share the same key using hibernate
回答1:
A map with multiple values for one key is known as a multimap - there's an implementation in the Apache commons library. Hibernate does not support this kind of collection directly, but it can be extended to do so relatively easily by implementing the UserCollectionType
interface. This blog article describes how to do it in detail.
回答2:
There is a multimap over on google collections. I don't know if they are hibernate compatible though.
回答3:
I have also found something relevent
http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/MultiMap.html#get(java.lang.Object)
So can prefer this also.
来源:https://stackoverflow.com/questions/475879/multimap-in-hibernate