我正在嘗試將Play 2.3.x用於小型項目。我習慣於1.2.x,並且正在努力理解一些變化。與Bean合併並播放Java的最佳方式
其中一個變化是使用EBeans和窗體。這個工作相當不錯,在1.2.x的,但我不明白怎麼和在2.3.x版本
做到這一點我有控制器:
package controllers;
import models.Device;
import play.data.DynamicForm;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import java.util.List;
public class Devices extends Controller {
public static Result index() {
List<Device> devices = Device.find.all();
return ok(views.html.Devices.index.render(devices));
}
public static Result add() {
Form<Device> myForm = Form.form(Device.class);
return ok(views.html.Devices.add.render(myForm));
}
public static Result edit(Long id){
Device device = Device.find.byId(id);
Form<Device> myForm = Form.form(Device.class);
myForm = myForm.fill(device);
return ok(views.html.Devices.edit.render(myForm));
}
public static Result update() {
Device device = Form.form(Device.class).bindFromRequest().get();
device.update();
return index();
}
}
我可以添加設備,並要對其進行編輯。這是模板:
@(myForm: Form[Device])
@main("Edit a device") {
@helper.form(action = routes.Devices.update()) {
@helper.inputText(myForm("name"))
@helper.inputText(myForm("ipAddress"))
<input type="submit" value="Submit">
}
<a href="@routes.Devices.index()">Cancel</a>
}
但我該如何合併與已存儲的對象的更改?有沒有簡單的方法,或者我是否已經找到對象,然後手動通過字段通過對象字段?在1.2.x(使用JPA)中有merge()選項來處理所有這些。我會使用JPA,但在1.2.x中的默認支持似乎並不強大。
現在,我得到(理解):
[OptimisticLockException: Data has changed. updated [0] rows sql[update device set name=?, ip_address=?, last_update=? where id=?] bind[null]]
發佈項目到GitHub上請,我會檢查它(只是假設這是一般測試項目w /出證書) – biesior
我發佈它在https://github.com/luukjansen/temperaturecontrol。 –
(有問題的表單是http:// localhost:9000/devices /其中可以添加一個) –