Issues using LinkedHashSet

88 views
Skip to first unread message

Shoaib

unread,
Aug 9, 2010, 9:38:30 PM8/9/10
to beanlib
Hello,
First of all, thanks for such a nice library.

We are using beanlib with great success. But recently we found issues
with the way beanlib handles LinkedHashSet.
At the backend we use Hibernate and most of our collections are mapped
as Set and we have been using Hibernate3Replicator to clone before
passing the object to UI.
Recently we introduced order-by in our hibernate mapping files to sort
in database on natural key. This caused issue with BeanLib as it
always converts the PersistentSet to HashSet regardless of the wrapped
implementation. Unfortunately we cannot convert our domain model to
use SortedSet and also we want the sorting to be done by hibernate.
Just to clarify more, if hibernate finds order by in mapping file it
creates LinkedHashSet instead of HashSet.

The custom transformer do not look a viable option here as it will
involve sorting in Java which we want to avoid. I wonder if it can be
done in following way ( both way beanlib needs to be changed)
1- Get the wrapped collection inside PersistentSet and use the same.
collection class in to object. Since hibernate does not provide a
Javabean friendly way to get the wrapped set, we need reflection at
field level.
2- Provide some customizations for such cases, like we cal always use
LinkedHashSet any time BeanLib try transforming a PersistentSet. Same
goes with all other collections where beanlib picks a default impl for
us.

I tried extending beanlib by inheritance but that is cubersome because
of static factories and we have to extend quite a number of classes to
change this default behavior. Als it required a lot of copy pasting
bean lib code.

I appreciate if someone can provide a better way of handling it or may
be some changes in beanlib will be helpful.

Thanks,
Shoaib

Hanson Char

unread,
Aug 11, 2010, 4:06:36 PM8/11/10
to bea...@googlegroups.com
>The custom transformer do not look a viable option here as it will
>involve sorting in Java which we want to avoid.

Why will it involve sorting ? If I understand this correctly, In a
custom transformer, you can create a LinkedHashMap as the target
instance, and then put the cloned elements in the same order as you
retrieve them which are already sorted.

Another alternative would be to plug in your own implementation of
MapReplicatorSpi (and the associated factory), via
BeanTransformer#initMapReplicatableFactory, to always create a
LinkedHashMap instead of HashMap as the default.

See MapReplicator.java for a reference implementation.

Hope this helps.

Hanson

Reply all
Reply to author
Forward
0 new messages