One way I've done it is with a DAO pattern.

    DAO dao = getDAO(request);
    Event event = dao.createEvent();

Event can either be the CayenneDataObject or it can be an interface
for one containing the setters and getters.

    event.setBlah();

I also stick queries on the DAO:

    List<Event> eventList = dao.findEventsByType(myType);

Another way I've done it is to make the business layer do the work and
the action only collects parameters, validates parameters, and passes
them to the business layer.

   businessLayer.processEvent(arg1, arg2, arg3, arg4, etc);



On Sat, Apr 30, 2011 at 3:46 PM, Tony Dahbura <[email protected]> wrote:
> I have an existing code base that is written around a mvc framework.&nbsp; 
> The code consists of actionbeans that call database methods to read data and 
> build out objects (e.g. UserRecord etc).&nbsp; The framework presents the 
> data and lets the user edit values and then when the actionbean is called 
> (during a form submit) it processes updating the database.
>
> I would like to reimplement the database work using Cayenne.
>
> My question is I do not want to expose the action beans (which are only 
> around during request scope not session) to have to deal with calling 
> Cayenne.  I was working to adjust my database class to handle the Cayenne 
> calls.  These methods take an argument of an object to write to the database 
> or update for example:
>
> public boolean insertNewEvent(Event event) {
>
> write event record to database
>
> }
>
>
> My code is beginning to look like this using cayenne:
>
> public boolean InsertNewEvent(Event event)  {
>    DataContext context = this.getContext();
>
>    Event ev = (Event)context.newObject(Event.class);
>
>    ev.setEventdate(event.getEventdate());
>    ev.setNotice(event.getNotice());
>    ev.setPkey(event.getPkey());
>    ev.setTimeend(event.getTimeend());
>    ev.setTimestart(event.getTimestart());
>    ev.setCategory(event.getCategory());
>    ev.setNotice(event.getNotice());
>    ev.setPkey(-1);
>
>    context.commitChanges();
>
>    return true;
> }
>
>
> I am finding myself creating an equivalent object using the context.newObject 
> call and copying the fields from my Event object to the one created by the 
> call to context.newObject.  Is there a better way to do this.  For instance 
> if I have been passed an event object that was not created with Cayenne 
> context call how do I make Cayenne pick it up and use it for the commit?  Or 
> should I have the action bean which is creating this new object call the 
> context.newObject and get back an event from cayenne (which I really was 
> hoping not to do (did not want to litter my action bean code with calls to 
> cayenne)).
>
> Is there a way to take an event object and make the cayenne context aware of 
> it so it can write or update it without copying the fields one by one?
>
> Thanks,
> Tony
>
>
>
>

Reply via email to