You can try objectContext.registerNewObject(event);

The tricky part is relationships.  Cayenne doesn't really have the notion of 
"detached" objects, the way hibernate does.  But as long as you are relating a 
new object to an existing object, cayenne will auto-add the new object to the 
existing object's data context, as long as the new object isn't already 
associated.

HTH

Robert

On Apr 30, 2011, at 4/302:46 PM , Tony Dahbura wrote:

> I have an existing code base that is written around a mvc framework.  
> The code consists of actionbeans that call database methods to read data and 
> build out objects (e.g. UserRecord etc).  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