CascadeFollowingCustomTransformer - a Bean Transformer which limits deep copying to cascading relationships

23 views
Skip to first unread message

Donnchadh Ó Donnabháin

unread,
Jan 17, 2011, 9:36:31 AM1/17/11
to bea...@googlegroups.com
Hi everyone,

Attached is a Bean Transformer which limits deep copying to cascading
relationships. Some may find it useful. The goal is to make deep
copying using beanlib-hibernate robust in the face of domain model
changes. In our case we tend to copy an aggregate root (as defined in
the Domain Driven Design literature) and all of its child entities,
mainly to support copying functionality in our application. It may be
of use to someone else.

At the moment this class isn't particularly efficient in that it
doesn't cache or pre-compute any information about the relationships
between entity classes. In our case performance is adequate since we
don't use it to copy very large numbers of objects.

Thoughts and feedback welcome.

Donnchadh

CascadeFollowingCustomTransformer.java

Hanson Char

unread,
Jan 17, 2011, 11:02:41 PM1/17/11
to bea...@googlegroups.com
Hi Donnchadh,

Not sure if I get this right. It seems to me (roughly) that the only
time it the isTransformable would return true is when it is an
association that doesn't have cascade SAVE_UPDATE enabled. And the
only transformation is a simple reference to the original object.

Can you provide some actual/sample use cases as to how one may find
this useful ? Or how this would make "deep copying robust in the face
of domain model changes" ?

Hanson

2011/1/17 Donnchadh Ó Donnabháin <donn...@gmail.com>:

Donnchadh Ó Donnabháin

unread,
Jan 18, 2011, 5:50:36 AM1/18/11
to bea...@googlegroups.com
Hi Hanson,

On 18 January 2011 04:02, Hanson Char <hanso...@gmail.com> wrote:
> Hi Donnchadh,
>
> Not sure if I get this right.  It seems to me (roughly) that the only
> time it the isTransformable would return true is when it is an
> association that doesn't have cascade SAVE_UPDATE enabled.  And the
> only transformation is a simple reference to the original object.

Yes, exactly. The idea is to keep the original reference in those
cases and only copy objects are part of the root object (aggregation)
as opposed to references to outside objects (association). The
convention that we use is that we cascade saves for aggregation but
not for associations.

> Can you provide some actual/sample use cases as to how one may find
> this useful ?  Or how this would make "deep copying robust in the face
> of domain model changes" ?

We use it to clone object in our system. We don't use
beanlib-hibernate as a mechanism to create DTOs (and we don't use
DTOs).
What we previously found was that as we added new associations to
classes in our domain model we had to remember to explicitly exclude
those relationships from deep copying by beanlib.

A slightly contrived example:
<hibernate-mapping package="com. ... .content">
<class name="Issue" table="cms_issues">
<id name="id">
<generator class="native" />
</id>
<version name="version" column="version"/>
<property name="title" />
<property name="summary" />
<property name="body" />
<many-to-one name="publication" column="publication_id"
class="Publication" />
<bag name="articles" inverse="true" cascade="all,delete-orphan"
order-by="id" >
<key column="issue_id" not-null="true" />
<one-to-many class="Article" />
</bag>
</class>

In the above example we want articles to be copied along with the
issue but we don't want the publication to be copied. If we add a new
association,
<many-to-one name="createdBy" column="created_by_user_id" class="User"/>
what would happen in the past was this would pull in a large tree of
objects referenced by the user object. With our transformer we can be
sure that the extent of deep copying is bounded.

Our domain model is much more complex than the simple example above,
with many domain classes and associations, so forgetting to exclude a
new association from deep-copying can cause real headaches.

Donnchadh

Hanson Char

unread,
Jan 19, 2011, 2:07:42 AM1/19/11
to bea...@googlegroups.com
Hi Donnchadh,

It makes sense to me now.  This sample class can potentially be very useful (at least as a reference implementation for doing other things that requires access to the underlying meta information).  Thanks for sharing.

Hanson

>We use it to clone object in our system. We don't use
>beanlib-hibernate as a mechanism to create DTOs (and we don't use
DTOs).

2011/1/18 Donnchadh Ó Donnabháin <donn...@gmail.com>

Donnchadh Ó Donnabháin

unread,
Jan 21, 2011, 7:13:12 AM1/21/11
to bea...@googlegroups.com
Unfortunately the first version didn't deal with components correctly.
I've attached a version which should correctly handle components
(including nested components).

Donnchadh

On 19 January 2011 07:07, Hanson Char <hanso...@gmail.com> wrote:
> Hi Donnchadh,

CascadeFollowingCustomTransformer.java

Hanson Char

unread,
Jan 22, 2011, 1:23:14 AM1/22/11
to bea...@googlegroups.com
Which version of Hibernate does this class depend on ?

2011/1/21 Donnchadh Ó Donnabháin <donn...@gmail.com>

Donnchadh Ó Donnabháin

unread,
Jan 23, 2011, 4:22:39 PM1/23/11
to bea...@googlegroups.com
It was developed against Hibernate 3.6.0 . Unfortunately it still
depends on Hibernate implementation classes and isn't as portable
between versions as I'd like.

Donnchadh

Reply all
Reply to author
Forward
0 new messages