You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by felix <fe...@technicalnorms.com> on 2011/04/27 14:36:07 UTC

commit all objects, temporary objects, POJO's, DTO

I am using Cayenne with Wicket, and am using the servlet filter described
here

http://cayenne.apache.org/doc30/web-applications.html

to bind a DataContext to requests. I understand this occurs per session, so
subsequent
requests within the session are associated to the same DataContext, which is
fine.

However, If I understand correctly, doing a commit against a DataContext
will persist _all_
objects associated to that context. If for instance, I am using Cayenne
objects to accumulate
data, but I do not want to persist these objects (eg because they have not
reached a consistent 
state) but I _do_ want to save some other object, then calling commit to
save this latter object will
save everything, including data that was not ready to be persisted.

I am not sure exactly what pattern I should be using with Cayenne. Should
Cayenne objects be exposed
beyond the persistent layer? I am considering using a Data Transfer Object
pattern, whereby Cayenne
objects only exist at the data layer, and data is exposed to the rest of the
application via
data transfer objects, which are a parallel structure of POJO's. In the case
of Wicket, this has
the additional benefit of solving potential problems with serialization that
may have been a problem
with Cayenne objects.

So, in summary, what is the correct usage of Cayenne? Is the DTO pattern I
have mentioned a reasonable
approach?

If DTO is a good path, should I be using the 'cgen' maven task to generate
POJO's? I have searched for
suitable velocity templates to be used with the 'cgen' task but have found
none. Should I write my own
or is something planned within the Cayenne project along these lines?

--
View this message in context: http://cayenne.195.n3.nabble.com/commit-all-objects-temporary-objects-POJO-s-DTO-tp2870230p2870230.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: commit all objects, temporary objects, POJO's, DTO

Posted by Mike Kienenberger <mk...@gmail.com>.
You're going to have to give us more information before we can
speculate on what's the best choice.   It may be that a combination of
several approaches make sense for your current project.

I've used a number of approaches in the past.   For example.....

On one struts project, I created a per-session data context but only
made changes to it per request.  At the end of every request, the data
context contained committed "read-only" objects.   The changes were
either committed or rolled back by the end of the request.   Similar
to your DTO approach , except that the persisted-between-request
values were persisted as separate items.

For a JSF project, I used a per-request data context, but serialized
all modified-but-uncommitted data objects and unserialized them to the
next per-request data context.   No DTO here -- the Cayenne Objects
were used at every layer.

What you do depends on how long you need to keep modified or cached
objects around (only one request (requeest scope), between requests
("page" scope), the lifetime of the session (session scope), the
lifetime of the application (application scope).

There's nothing stopping you from using a combination of choices.  For
some things, create a data context on demand.   For other things, use
a session-scoped data context.


On Wed, Apr 27, 2011 at 10:54 AM, felix
<fe...@technicalnorms.com> wrote:
> Thanks for your quick response.
>
> It seems like we have two options:
> - Create a context per writeable action (otherwise we could not retrieve an
> object, updated and save exactly when we want, in other words, when the obj
> is in a consistent state)
> - Create a DTO layer and have for instance one context per session. The most
> flexible option so far.
>
> I have heard there is something related to POJOs in Cayenne, Could I have
> more information regarding this point (I've tried but no much info on this)?
>
> Thanks a lot
>
>
> --
> View this message in context: http://cayenne.195.n3.nabble.com/commit-all-objects-temporary-objects-POJO-s-DTO-tp2870230p2870619.html
> Sent from the Cayenne - User mailing list archive at Nabble.com.
>

Re: commit all objects, temporary objects, POJO's, DTO

Posted by felix <fe...@technicalnorms.com>.
Thanks for your quick response.

It seems like we have two options:
- Create a context per writeable action (otherwise we could not retrieve an
object, updated and save exactly when we want, in other words, when the obj
is in a consistent state) 
- Create a DTO layer and have for instance one context per session. The most
flexible option so far.

I have heard there is something related to POJOs in Cayenne, Could I have
more information regarding this point (I've tried but no much info on this)?

Thanks a lot


--
View this message in context: http://cayenne.195.n3.nabble.com/commit-all-objects-temporary-objects-POJO-s-DTO-tp2870230p2870619.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: commit all objects, temporary objects, POJO's, DTO

Posted by Mike Kienenberger <mk...@gmail.com>.
The thing to remember is that Cayenne DataContexts are completely
under your control.   There are convenience classes to automatically
make one data context managed per session, but that's only one of many
ways, not only way or even the recommended way.

It's hard to help without more specific information, but you could
create a temporary one-time-use data context for your "some other
object" below.

Or you could create per-request data contexts, serialize the data
objects you haven't finished with, and unserialize them into the next
per-request data context.

At least in 2.0, it was possible to serialize DataObjects (without the
backing DataContext), and then reattach them to a new DataContext.

Or you could have a session-scoped or application-scoped read-only
context (by convention), then create a new data context whenever you
wanted to make changes, and keep it around only as long as those
changes were needed.


On Wed, Apr 27, 2011 at 8:36 AM, felix <fe...@technicalnorms.com> wrote:
> I am using Cayenne with Wicket, and am using the servlet filter described
> here
>
> http://cayenne.apache.org/doc30/web-applications.html
>
> to bind a DataContext to requests. I understand this occurs per session, so
> subsequent
> requests within the session are associated to the same DataContext, which is
> fine.
>
> However, If I understand correctly, doing a commit against a DataContext
> will persist _all_
> objects associated to that context. If for instance, I am using Cayenne
> objects to accumulate
> data, but I do not want to persist these objects (eg because they have not
> reached a consistent
> state) but I _do_ want to save some other object, then calling commit to
> save this latter object will
> save everything, including data that was not ready to be persisted.
>
> I am not sure exactly what pattern I should be using with Cayenne. Should
> Cayenne objects be exposed
> beyond the persistent layer? I am considering using a Data Transfer Object
> pattern, whereby Cayenne
> objects only exist at the data layer, and data is exposed to the rest of the
> application via
> data transfer objects, which are a parallel structure of POJO's. In the case
> of Wicket, this has
> the additional benefit of solving potential problems with serialization that
> may have been a problem
> with Cayenne objects.
>
> So, in summary, what is the correct usage of Cayenne? Is the DTO pattern I
> have mentioned a reasonable
> approach?
>
> If DTO is a good path, should I be using the 'cgen' maven task to generate
> POJO's? I have searched for
> suitable velocity templates to be used with the 'cgen' task but have found
> none. Should I write my own
> or is something planned within the Cayenne project along these lines?
>
> --
> View this message in context: http://cayenne.195.n3.nabble.com/commit-all-objects-temporary-objects-POJO-s-DTO-tp2870230p2870230.html
> Sent from the Cayenne - User mailing list archive at Nabble.com.
>

Re: commit all objects, temporary objects, POJO's, DTO

Posted by Michael Gentry <mg...@masslight.net>.
Hi Felix,

I use DataContexts to group related changes.  If I need to do an
unrelated change, I create a new (or child) DataContext, do my change
in it, and commit it.  You don't have to stick to just one
DataContext.  Create as many as you need.

mrg


On Wed, Apr 27, 2011 at 8:36 AM, felix <fe...@technicalnorms.com> wrote:
> I am using Cayenne with Wicket, and am using the servlet filter described
> here
>
> http://cayenne.apache.org/doc30/web-applications.html
>
> to bind a DataContext to requests. I understand this occurs per session, so
> subsequent
> requests within the session are associated to the same DataContext, which is
> fine.
>
> However, If I understand correctly, doing a commit against a DataContext
> will persist _all_
> objects associated to that context. If for instance, I am using Cayenne
> objects to accumulate
> data, but I do not want to persist these objects (eg because they have not
> reached a consistent
> state) but I _do_ want to save some other object, then calling commit to
> save this latter object will
> save everything, including data that was not ready to be persisted.
>
> I am not sure exactly what pattern I should be using with Cayenne. Should
> Cayenne objects be exposed
> beyond the persistent layer? I am considering using a Data Transfer Object
> pattern, whereby Cayenne
> objects only exist at the data layer, and data is exposed to the rest of the
> application via
> data transfer objects, which are a parallel structure of POJO's. In the case
> of Wicket, this has
> the additional benefit of solving potential problems with serialization that
> may have been a problem
> with Cayenne objects.
>
> So, in summary, what is the correct usage of Cayenne? Is the DTO pattern I
> have mentioned a reasonable
> approach?
>
> If DTO is a good path, should I be using the 'cgen' maven task to generate
> POJO's? I have searched for
> suitable velocity templates to be used with the 'cgen' task but have found
> none. Should I write my own
> or is something planned within the Cayenne project along these lines?
>
> --
> View this message in context: http://cayenne.195.n3.nabble.com/commit-all-objects-temporary-objects-POJO-s-DTO-tp2870230p2870230.html
> Sent from the Cayenne - User mailing list archive at Nabble.com.
>