Updating an object to the database using Apache Cayenne

孤街浪徒 提交于 2019-12-14 03:59:51

问题


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.


回答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.



来源:https://stackoverflow.com/questions/14430205/updating-an-object-to-the-database-using-apache-cayenne

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!