Transformations

20 views
Skip to first unread message

Luis

unread,
May 31, 2012, 10:34:39 AM5/31/12
to beanlib
Hi.

First of all, I'd like to thank you for your library. It's very
useful.

I've been playing with it, trying to copy a Hiberanate object,
ignoring some properties of the beans and resetting some values to a
concrete value. using the HibernateBeanReplicator.

While the copy and the use of the HibernatePropertyFilter are working
properly, I'm trying to set certains properties of the bean, and all
the beans included, recursively, without success.

Is there any extension point in the library that lets me set values to
certains properties of all the beans copied during the copy process?

I've tried with a CustomBeanTransformerSpi transformer, where I set
the values, but that doesn't seem to be the purpose of that class, as
I don't want a replacment of Hibernate3BeanTransformer but an
extension, and I want to run the sets over all the copied beans, not
only the first level bean.

Thaks in advance.

Hanson Char

unread,
May 31, 2012, 12:33:30 PM5/31/12
to bea...@googlegroups.com
It seems CustomBeanTransformerSpi is the extension point you are looking for.

The intent of CustomBeanTransformerSpi instance is to alter the
transformation behavior - it doesn't replace Hibernate3BeanTransformer
unless you choose to always return true from your provided
CustomBeanTransformerSpi.isTransformable() method.

There also exists a convenient ChainedCustomBeanTransformer.java that
makes it easier to "combine" multiple custom transformers:

http://beanlib.sourceforge.net/5.0.5/api/net/sf/beanlib/spi/ChainedCustomBeanTransformer.html

More general info:

http://beanlib.svn.sourceforge.net/viewvc/beanlib/trunk/beanlib-doc/faq.html

Hope this helps.

Regards,
Hanson

Luis

unread,
May 31, 2012, 12:55:23 PM5/31/12
to beanlib
Thanks for your response.

When I use the HibernatePropertyFilter, both of them, the
Hibernate3BeanTransformer and the HibernatePropertyFilter are taken
into account: the Hibernate3BeanTransformer iterates over the
properties and copies only the properties that are not excluded by the
filter, and so on for all the objects.

I need the same to initialise values on fields, and if I use a custom
transformer, I fear that the Hibernate3BeanTransformer is not going to
be run to iterate over the beans.

Basically, for each bean copied, by the Hibernate3BeanTransformer
execution, I need overwrite some properties before going on with the
next bean.

The entity structure is so big that I've discarded to create a
CustomBeanTransformerSpi to perform the deep transformation there (on
the top level object), as I'd be replacing the deep copy process.


Hanson Char

unread,
May 31, 2012, 1:07:54 PM5/31/12
to bea...@googlegroups.com
Hi Luis,

> if I use a custom
> transformer, I fear that the Hibernate3BeanTransformer is not going to
> be run to iterate over the beans.

Note the factory method signature of newCustomBeanTransformer of
CustomBeanTransformerSpi.Factory:

public CustomBeanTransformerSpi
newCustomBeanTransformer(BeanTransformerSpi contextBeanTransformer);

The input parameter "contextBeanTransformer" is the original/internal
transformer used, so what you can do is to save it as a member field
in your custom transformer(s), and delegate the transformation to it
as needed. In other words, "Hibernate3BeanTransformer" can be
controlled to run as needed.

Hope this helps.

Hanson

Luis

unread,
May 31, 2012, 1:09:21 PM5/31/12
to beanlib
I think that the additivity of the two behaviours could be got with
the next change on BeanTransformer:

@Override
public final <T> T transform(Object from, Class<T> toClass,
PropertyInfo propertyInfo)
{
try {
T replica = replicate(fromReplication, toClass);
if (customTransformer.isTransformable(from, toClass,
propertyInfo))
return customTransformer.transform(replica, toClass,
propertyInfo);
else
return replica;
} catch (SecurityException e) {
throw new BeanlibException(e);
}
}

Additionally, if the coder just want to use the customTransformer,
without the Hibernate3BeanTransformer, he/she can set up the
HibernatePropertyFilter to exclude the javabean from the replication
process.


On May 31, 5:33 pm, Hanson Char <hanson.c...@gmail.com> wrote:
> It seems CustomBeanTransformerSpi is the extension point you are looking for.
>
> The intent of CustomBeanTransformerSpi instance is to alter the
> transformation behavior - it doesn't replace Hibernate3BeanTransformer
> unless you choose to always return true from your provided
> CustomBeanTransformerSpi.isTransformable() method.
>
> There also exists a convenient ChainedCustomBeanTransformer.java that
> makes it easier to "combine" multiple custom transformers:
>
>  http://beanlib.sourceforge.net/5.0.5/api/net/sf/beanlib/spi/ChainedCu...
>
> More general info:
>
>  http://beanlib.svn.sourceforge.net/viewvc/beanlib/trunk/beanlib-doc/f...
>
> Hope this helps.
>
> Regards,
> Hanson
>
>
>
> On Thu, May 31, 2012 at 7:34 AM, Luis <luch...@gmail.com> wrote:
> > Hi.
>
> > First of all, I'd like to thank you for your library. It's very
> > useful.
>
> > I've been playing with it, trying to copy a Hiberanate object,
> > ignoring some properties of the beans and resetting some values to a
> > concrete value. using the HibernateBeanReplicator.
>
> > While the copy and the use of the HibernatePropertyFilter are working
> > properly, I'm trying to set certains properties of the bean, and all
> > the beans included, recursively, without success.
>
> > Is there any extension point in the library that lets me set values to
> > certains properties of all the beans copied during the copy process?
>
> > I've tried with a CustomBeanTransformerSpi transformer, where I set
> > the values, but that doesn't seem to be the purpose of that class, as
> > I don't want a replacment of Hibernate3BeanTransformer but an
> > extension, and I want to run the sets over all the copied beans, not
> > only the first level bean.
>
> > Thaks in advance.- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages