如何告诉Play Framework 2和Ebean保存空字段? [英] How do I tell Play Framework 2 and Ebean to save null fields?
问题描述
我正在使用Play Framework 2和Ebean.当用户提交表单以编辑数据库中的现有对象时,它不会保存空值.我想这是为了防止覆盖非null形式的字段.但是,如果需要,我如何让他们将表单中的字段设置为null?
I'm using Play Framework 2 and Ebean. When a user submits a form to edit an existing object in the database, it doesn't save null values. I guess this is to prevent overwriting fields that aren't in the form with null. But how can I let them set fields in the form to null if they need to?
例如,用户编辑Event
对象. Event.date
是1/1/13.用户将表单中的Event.date
字段设置为空,然后提交表单.在调试器中检查Event.date
会显示其值为null.我保存了Event
.如果我查看数据库中的Event
,它的值仍然是1/1/13.
For example, the user edits an Event
object. Event.date
is 1/1/13. The user sets the Event.date
field in the form to empty and submits the form. Inspecting Event.date
in the debugger shows its value is null. I save the Event
. If I look at the Event
in the database, its value is still 1/1/13.
似乎有一种解决方法.唯一的问题是它不适用于嵌套实体.有什么解决办法吗?
It seems there is a method for this. The only problem is it doesn't work on nested entities. Any solutions for this?
update(Object bean,Set<String> properties)
推荐答案
在application.conf
文件旁边创建一个ebean.properties
文件,并将以下行添加到其中:
Create an ebean.properties
file right next to the application.conf
file and add this line to it:
ebean.defaultUpdateNullProperties=true
这篇关于如何告诉Play Framework 2和Ebean保存空字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!