1

I am using Cayenne in a project for the first time. Till now, i was using the SelectQuery and was loving it. I now need to update an object e.g. my User object contains an emailId attribute. When the user needs to update his/her email, i take the existing User object and update the emailId attribute with the new value provided by the user.

The problem starts now, where i don't understand the way to persist the update to the database. The options i have seem to be limited to SQLTemplate or using EJB QL. Am I right? Is there a more elegant way of supplying the updated object to the DataContext and persisting the update to the DB?

I am using Cayenne in a web application and obtain the context via the WebApplicationContextFilter.

user1781912
  • 43
  • 1
  • 1
  • 5

1 Answers1

1

yes, there is certainly a more elegant way. You make one or more changes to your objects, you then commit them via the ObjectContext that was used to get the objects in the first place:

ObjectContext context = ...
List<MyEntity> objects = context.performQuery(...); 
MyEntity o = objects.get(0);  
o.setXyz("new value"); // I assume you got to this point
...
context.commitChanges();

The last line sends all your changes to the DB.

andrus_a
  • 2,528
  • 1
  • 16
  • 10
  • Thanks Andrus. I like Cayenne a lot but I wish the documentation was more thorough, especially when using it in a web application. I realized the answer as soon as I realized that the Context is bound to the session. However thanks so much for responding. Is there a good example of where the Context is bound to the request and what are the performance trade off's of binding the Context to the request as opposed to the session? Is the context tied to a single database connection? If so, what should be a good configuration of the connection pool? – user1781912 Jan 23 '13 at 22:32
  • Sorry, just noticed your comment here. Feel free to open more top-level questions if these are still unclear. – andrus_a Mar 20 '13 at 06:13