[hibernate-dev] Implement UserCollectionType for non-Collection types such as Guava's Multimap

Jan-Willem Gmelig Meyling jan-willem at youngmediaexperts.nl
Mon Apr 24 14:47:41 EDT 2017


Hi everyone,

Out of curiosity I’ve tried to implement a UserCollectionType for Guava’s Multimap [1] based on this [2] article thats doing a similar job for Apache’s MultiMap. Doing so I’ve stumbled upon two issues, for which I’d like to receive some feedback.

As far as I can see, the UserCollectionType seems focussed around mapping values to indexes. In the case of a list this would supposedly be a number, for non-ordered collections I am not sure, but for maps it seems to be the entries' key. It seems that the implementation for Apache’s MultiMap actually is a Map<K, Collection<V>> so that in the UserCollectionType mentioned in that article every index actually points to a collection of values under that key. I am wondering whether this could potentially break the behaviour for the UserCollectionType class as it seems it should be possible to dirty check the values behind the indexes which probably doesn’t work against collections.

But there’s another inconvenience I stumbled upon while implementing this. Where the Apache MultiMap extends a Map<K, Object>, Guava’s Multimap does not. I therefore had to override the TypeUtils, JavaXCollectionType, CollectionBinder, ArrtirubteFactory, PluralAttributeImpl, and most likely if I wanted to do it well a MapBinder and PluralAttribute’s CollectionType as well. While doing this I also found out that JavaXCollectionType.getCollectionClass() returns Map.class perfectly fine for the currently supported Map fields, even though they do not satisfy the <? extends Collection> return type.

So it seems this is a bad idea after all ;-) But I am wondering whether I am missing something here.

a) Is there a way to map a Map<Key, Collection<Value>> as an EnumeratedCollection in plain Hibernate?
b) Is there another way to achieve the above and persist a Guava Multimap with a custom user type? (Assuming the UserCollectionType is not the right place to start)
c) Is Hibernate 6 going to change the answer to these questions?
d) Was the article ever right about how to persist Apache’s MultiMap’s type, or was it based on false assumptions on the use of UserCollectionType anyways?
 
The code I’ve written for this email can be found under [3].

Feedback is much appreciated, thanks in advance!

Kind regards,

Jan-Willem

[1] http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/Multimap.html <http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/Multimap.html>
[2] http://blog.xebia.com/mapping-multimaps-with-hibernate/ <http://blog.xebia.com/mapping-multimaps-with-hibernate/>
[3] https://github.com/JWGmeligMeyling/hibernate-multimap-type/tree/master/guava-hibernate5-types <https://github.com/JWGmeligMeyling/hibernate-multimap-type/tree/master/guava-hibernate5-types>



More information about the hibernate-dev mailing list