是否可以配置 Play Framework 的 CRUD 模块以遵守 @Column(unique=true) 注释?
我正在使用 Play 的 CRUD 模块创建一组简单的管理屏幕。我的模型之一是用户,我想对电子邮件字段强制执行唯一约束。
代码如下所示:
public class User extends Model {
@Email
@Required
@Column(unique=true)
public String email;
管理屏幕正确显示 - 当我尝试打破唯一性(通过使用已使用的电子邮件保存用户)时,我收到此错误(在浏览器中):
Execution exception
PersistenceException occured : org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
In {module:crud}/app/controllers/CRUD.java (around line 100)
96:
} catch (TemplateNotFoundException e) {
97:
render("CRUD/show.html", type, object);
98:
}
99:
}
100:
<b>object._save();</b>
101:
flash.success(Messages.get("crud.saved", type.modelName));
102:
if (params.get("_save") != null) {
103:
redirect(request.controller + ".list");
104:
}
105:
redirect(request.controller + ".show", object._key());
106:
}
我可以进行任何调整来使用 CRUD模块和列唯一性注释?
I'm using Play's CRUD module to create a simple set of admin screens. One of my models is User and I want to enforce a unique constraint on the email field.
The code looks like this:
public class User extends Model {
@Email
@Required
@Column(unique=true)
public String email;
The admin screen displays correctly - when I try to break uniqueness (by saving a user with an already used email) I get this error (in the browser):
Execution exception
PersistenceException occured : org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
In {module:crud}/app/controllers/CRUD.java (around line 100)
96:
} catch (TemplateNotFoundException e) {
97:
render("CRUD/show.html", type, object);
98:
}
99:
}
100:
<b>object._save();</b>
101:
flash.success(Messages.get("crud.saved", type.modelName));
102:
if (params.get("_save") != null) {
103:
redirect(request.controller + ".list");
104:
}
105:
redirect(request.controller + ".show", object._key());
106:
}
Are there any tweaks I can make to use the CRUD module AND column uniqueness annotations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建自定义检查并将其添加到 User 类中的电子邮件属性中。
然后
You can create a custom check and add to the email property in the User class.
Then
在我看来,系统正在按照您的预期进行操作。我唯一能想到的是修补 CRUD 模块以捕获此异常并变得更好,但通常唯一的约束是数据库端约束,只能通过尝试写入数据库来检查。因此,唯一约束不是验证注释(可以在写入数据库之前执行)。
It sounds to me like the system is doing just what you expected. The only thing I can imagine is patching the CRUD module to catch this exception and be nicer, but generally unique constraints are database-side constraints and can only be checked by trying to write to the database. So a unique constraint is not a validation annotation (which can be executed before writing to the database).