vendredi 29 juillet 2016

PlayFramework 2.5 - Ebean update using reflection


I'm developing a website with an inline editor using Play framework 2.5 and Ebean as ORM, and I have a news section where the admin can edit every single news (editing fields inline such as title, content and so on).
In order to do so, I set every html element which can be modified with an id equals to the news model field (e.g. the html element mapping the field title will have id="title"), then when I receive data from the client, I use reflection on the controller to map every content with the correct news field.

Here is the code (EditContent is an object which contains informations like the id and the htmlContent of every modified content):

News news = News.find.byId(newsId);

for(EditContent content : pageContents.contents) {
    Field field = news.getClass().getField(content.cssId);
    field.setAccessible(true);
    field.set(news, content.htmlContent);
}

news.update();

The problem is that the update seems to be executed, but actually values are not updated on db. Using debugger, I inspected the object news and I can see that the fields are modified properly, but then the update has no effects on db.
Also, I noticed that the same code using:

News news = new News()
...
//reflection to save modifed contents in the new object
...
news.save()

works as I expect, saving a new row in the database.

Any idea?

Thank you in advance for your help!





Aucun commentaire:

Enregistrer un commentaire