准备 JPA 实体以供 HTML 表单使用
假设我想使用用户通过 Web 应用程序表单(Tapestry)提供的值来填充 JPA 实体。
获取将绑定到表单字段的 JPA 实体的“空白”实例的最佳方法是什么?
到目前为止,我只是在 Tapestry 类中使用 new
运算符,如下所示:
childminderAccount = new ChildminderAccount();
这不是一种粗略的做法吗?有更好的办法吗?
Say I want to populate a JPA entity using values supplied by a user through a web application form (Tapestry for that matter).
What is the best way to obtain the "blank" instance of the JPA entity that is going to be bound to the form fields?
As of now I just use the new
operator as follows in my Tapestry class:
childminderAccount = new ChildminderAccount();
Is this not a somewhat a crude way of doing it? Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是最好的方法。 JPA(相对于旧的 EJB 持久性)的优点之一是它是一个“更轻量”的框架。它的优点之一是它现在可以与 POJO(或 Java Bean)一起工作。不过,我建议您看一下 Java 的新验证 API,它也非常轻量级,它可以确保从表单中正确填充 JPA Bean(例如,不可为空的字段设置为 null、空 id 字段等) :
http://www.hibernate.org/subprojects/validator.html
Nope, that's the best way to do it. One of the advantages of JPA (over old EJB Persistence) is that it is a "lighter" framework. One of it's lightnesses is the fact that it now works woth POJOs (or Java Beans). I would recommend however to take a look at Java's new validation API which is very lightweight as well, and it can insure that a JPA Bean is correctly populated from your form (like no non-nullable fields set to null, empty id field etc):
http://www.hibernate.org/subprojects/validator.html