EBeans update does not save changed field items

81 Views Asked by At

I upgrade from Play 2.5 to 2.7, and am having a problem with saving my forms. When fields are changed and I call the Model.update() the changes are not persisted in the database (even though they show changed when debugging before the update is done)

When however I set them specifically, then they do persists. So it must have to do something with the fact that it does not detect the change and does not see the object as changed. I use getter and setters in the model, and all the properties are private.

This is the controller function (with the two lines to persist those two fields)

@Check(UserTask.MANAGER)
public Result updateSceneSet(Http.Request request) {
    Messages messages = messagesApi.preferred(request);
    Form<StreamingSceneSet> form = formFactory.form(StreamingSceneSet.class).bindFromRequest(request);
    if (form.hasErrors()) {
        if (form.rawData().get("id") != null && form.rawData().get("id").length() > 0) {
            long itemId = Long.parseLong(form.rawData().get("id"));
            StreamingSceneSet item = StreamingSceneSet.findById(itemId);
            return badRequest(views.html.streaming.editSceneSetView.render(form, item, messages, request));
        } else {
            return badRequest(views.html.streaming.createSceneSetView.render(form,messages, request));
        }
    }

    // Form is OK, has no errors we can proceed
    StreamingSceneSet item = form.get();

    item.setName(item.getName());
    item.setDescription(item.getDescription());

    // Insert or update?
    if (item.getId() == null) {
        item.insert();
        flash("success", messages().at("addedSceneSet", item.getName()));
    } else {
        item.update();
        flash("success", messages().at("updatedSceneSet", item.getName()));
    }
    return redirect(routes.Streaming.sceneSets());
}
1

There are 1 best solutions below

0
Luuk D. Jansen On

It seems because when I started the upgrade I had some legacy classes I didn't have getters and setters, and as I had some issue I put in:

play.forms.binding.directFieldAccess = true

Removing this made everything work again.