You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Tony Dahbura <da...@aopslab.com> on 2011/04/30 21:46:11 UTC

Help with a newbie question

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 




Re: Help with a newbie question

Posted by Tony Dahbura <da...@aopslab.com>.
Thanks I will give it a try.  The new object also has other new objects as part of its creation so my assumption is they will get added correctly as well?

Let me try it and will report back to the user group on the findings.

Thanks,
Tony




----- Original Message -----
From: Robert Zeigler <ro...@roxanemy.com>
To: user@cayenne.apache.org
Sent: Sat, 30 Apr 2011 16:21:46 -0400 (EDT)
Subject: Re: Help with a newbie question

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.&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 
> 
> 
> 



Re: Help with a newbie question

Posted by Robert Zeigler <ro...@roxanemy.com>.
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.&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 
> 
> 
> 


Re: Help with a newbie question

Posted by Mike Kienenberger <mk...@gmail.com>.
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 <da...@aopslab.com> 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
>
>
>
>