与 Netbeans 和 JSF 建立 ManyToMany 关联的脚手架
我正在使用 NetBeans 及其 RAD 开发功能开发 JEE6 JSF 应用程序。我想使用脚手架来节省从模型更新控制器和视图的时间。在视图中生成 OneToMany 关联没有任何问题(作为一个下拉列表,让您选择父实体),但不会生成 ManyToMany 关联。
您知道一种构建多对多关联的方法吗?您在 NetBeans 中使用哪些 RAD 技术? (您推荐的插件、教程、材料)
I'm developing a JEE6 JSF application using NetBeans and its RAD development features. I want to use scaffolding to save time updating controllers and views from Models. The OneToMany associations are generated in the views without any problem (as a dropdown that let you choose the parent entity) but the ManyToMany associations aren't generated.
Do you know a way to scaffold ManyToMany associations? Which RAD techniques do you use with NetBeans? (plugins, tutorials, materials you recommend)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
您可以尝试 Netbeans 7.0 中的替代方案,即使用 JSP 作为视图技术而不是 Facelets (XHTML)。如果这样做,IDE 将为托管 Bean 生成不同的代码,并添加一个实用程序类:JsfElResolver.java,它将在每个用户操作时调用,以允许在父级和子级之间导航,就像提交表单一样。
在这种情况下,Many2Many 关系得到部分支持,但如果您的数据库设计良好,它通常会起作用。
正如您所说的插件,在本例中使用 PrimeFaces 或 OpenFaces 作为视图库。这两个库不需要痛苦的配置,并且可以与 Netbeans 中的 JSF CRUD 脚手架完美配合。除了 Primefaces 数据表过滤和排序之外,这在使用 DataModel 时是有问题的。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
实际上,无论数据库供应商是什么,带有 Netbeans 的 JSF 支架尚不能管理 Many2Many 关系。数据库视图也是如此。
正如您所注意到的,当您想要从数据库生成实体时,只有具有 ID(主键)的表才会被接受。然而,向导将识别连接表并正确注释实体。看起来(Netbeans 7.0)
示例:
通过此链接:http://wiki.netbeans.org/JsfCrudGenerator
您应该注意到,如果需要的话,如果它们与上面的示例不同,则必须修改在生成 JSF 页面(托管 Bean 和 JPA 控制器)之前生成的实体类。
由于 EclipseLink 是 Netbeans 使用的默认 JPA 提供器,因此您应该检查 EclipseLink 站点以查看有关如何在实体类中定义多对多关系的示例,并了解如何在 JPA 控制器 (Java-ee-5) 或无状态会话中管理它您生成的 bean (Java-EE-6)。
视图部分的逻辑方法是添加到创建表单中,例如:
但不幸的是,这不起作用并且将返回:无效值。虽然这似乎是合乎逻辑的:将 Writer 对象的集合添加到 Topic 对象。
正如您所看到的,多对多关系由实体类中的集合表示,因此这里的问题是如何将集合添加到对象。
我从未在 JSF 环境中处理过“多对多”关系,因此我现在无法判断如何有效地进行这些修改。但你可以直接询问 Netbeans.org 的 JSF 团队。或者看看 SpringFuse 的人如何使用 Spring 解决这个问题(实际上他们在 m2m 相关的实体类中添加了
addObject(){}
和removeObject(){}
方法 但是如果您不介意在您的应用程序中使用 Spring 框架,您可以尝试他们的在线 JSF Web 应用程序生成器并检查生成的代码以了解其之间的差异。
,您可以查看此博客文章(教程),这解释了一种支持方式Netbeans (Java-EE-6) 中的Many2Many 关系使用一个简单的示例(Book - AuthorBook - Author)。
http://ikennaokpala.wordpress.com/ 2010/06/18/持久化jpa多对多关系/
Actually, the JSF scaffolding with Netbeans is not yet capable of managing Many2Many relationships whatever the Database vendor is. Same thing for database Views.
As you you can notice, when you want to generate Entities from Database, only tables with an ID (primary key) will be accepted. Yet the wizard will recognize the join tables and will annotate the entities correctly. Seemingly (Netbeans 7.0)
Example:
Through this link : http://wiki.netbeans.org/JsfCrudGenerator
You should notice that you have to modify the Entity classes generated before generating the JSF pages (Managed Beans and JPA controllers), if needed, if they are different from the example above.
As EclipseLink is the default JPA profider used by Netbeans, you should check the EclipseLink site to see examples on how ManytoMany relationship is defined in entity Classes, and see how this is managed in the JPA controller (Java-ee-5) or Stateless Session beans (Java-EE-6) you generated.
The logical way for the view part was to add in a Create form for example :
But unfortunately that will not work and will return : Unvalid value. While this seems to be logical : Adding a collection of Writer objects to a Topic object.
As you can see, the Many2Many relationship is represented by a Collection in the Entity Class so the deal here is how to add a collection to an object.
I never dealt with 'many to many' relationships in a JSF environment so I can't tell for now how to bring these modifications efficiently. But you may directly ask the JSF Team of Netbeans.org. Or see how the guys from SpringFuse resolved the issue using Spring (Actually they added
addObject(){}
andremoveObject(){}
methods in the entity classes concerned by the m2m relationship.You can try their online JSF Web app generator and check the generated code to see the difference between theirs and yours, if you don't mind using Spring Framework to you application.
However, you can check this Blog article (tutorial), that explains a way to support Many2Many relationship in Netbeans (Java-EE-6) using a simple example (Book - AuthorBook - Author).
http://ikennaokpala.wordpress.com/2010/06/18/persisting-jpa-many-to-many-relationship/