A solution to replicate object which is referenced in multiple places

24 views
Skip to first unread message

qin

unread,
Jun 11, 2012, 2:41:28 PM6/11/12
to beanlib

I have a very complicated object to clone. Basically the object has
many children and grand children and they reference each other. I
can't change that relationship.

While using beanlib to clone the object, I found that when an instance
is referenced at multiple places, it will be cloned multiple times,
which cause problem when persisting the object to database because
hibernate tries to create the same database entry multiple times.

So the solution is to share the same cloned instance at multiple
places where it is referenced.

Here is my changes in
net.sf.beanlib.provider.replicator.BeanReplicator:replicateBean(...):

T to;
+ if (getTargetCloned(from) != null) {
+ // The object (from) is already cloned, and is shared in
multiple places. Don't make a new clone!
+ return (T) getTargetCloned(from);
+ }
try {
to = createToInstance(from, toClass);
} catch (SecurityException e) {

It seems to solve the problem. Now I can clone and persist my object
without problem.

Thanks for beanlib to provide such a useful tool to solve my problem.

Hanson Char

unread,
Jun 11, 2012, 4:14:27 PM6/11/12
to bea...@googlegroups.com
This doesn't sound right. The default behavior of Beanlib reuses the
same "from" instance if there are multiple references to it. Which
version of beanlib are you using ?

h
Reply all
Reply to author
Forward
0 new messages