与 Netbeans 和 JSF 建立 ManyToMany 关联的脚手架

发布于 11-15 16:17 字数 205 浏览 10 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻许诺言2024-11-22 16:17:13

实际上,无论数据库供应商是什么,带有 Netbeans 的 JSF 支架尚不能管理 Many2Many 关系。数据库视图也是如此。

正如您所注意到的,当您想要从数据库生成实体时,只有具有 ID(主键)的表才会被接受。然而,向导将识别连接表并正确注释实体。看起来(Netbeans 7.0)
示例:

@JoinTable(name = "TOPIC_WRITER", joinColumns = {
        @JoinColumn(name = "topicname", referencedColumnName = "topicname")}, inverseJoinColumns = {
        @JoinColumn(name = "writerid", referencedColumnName = "writerid")})
    @ManyToMany
    private List<Writer> writerList;

@ManyToMany(mappedBy = "writerList")
    private List<Topic> topicList;

通过此链接:http://wiki.netbeans.org/JsfCrudGenerator
您应该注意到,如果需要的话,如果它们与上面的示例不同,则必须修改在生成 JSF 页面(托管 Bean 和 JPA 控制器)之前生成的实体类。

由于 EclipseLink 是 Netbeans 使用的默认 JPA 提供器,因此您应该检查 EclipseLink 站点以查看有关如何在实体类中定义多对多关系的示例,并了解如何在 JPA 控制器 (Java-ee-5) 或无状态会话中管理它您生成的 bean (Java-EE-6)。

视图部分的逻辑方法是添加到创建表单中,例如:

<h:selectManyMenu id="writer" value="#{topicController.selected.writerCollection}" title="#{bundle.CreateBookTitle_writer}" required="true" requiredMessage="Writer needed">
                        <f:selectItems value="#{writerController.itemsAvailableSelectMany}"/>
                    </h:selectManyMenu>

但不幸的是,这不起作用并且将返回:无效值。虽然这似乎是合乎逻辑的:将 Writer 对象的集合添加到 Topic 对象。

正如您所看到的,多对多关系由实体类中的集合表示,因此这里的问题是如何将集合添加到对象

我从未在 JSF 环境中处理过“多对多”关系,因此我现在无法判断如何有效地进行这些修改。但你可以直接询问 Netbeans.org 的 JSF 团队。或者看看 SpringFuse 的人如何使用 Spring 解决这个问题(实际上他们在 m2m 相关的实体类中添加了 addObject(){}removeObject(){} 方法 但是

/**
     * Returns the roles List.
     */
    @Cache(usage = NONSTRICT_READ_WRITE)
    @JoinTable(name = "ACCOUNT_ROLE", joinColumns = @JoinColumn(name = "ACCOUNT_ID"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    @ManyToMany(cascade = PERSIST)
    public List<Role> getRoles() {
        return roles;
    }

    /**
     * Set the roles List.
     * It is recommended to use the helper method addRole /  removeRole
     * if you want to preserve referential integrity at the object level.
     *
     * @param roles the List of Role
     */
    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    /**
     * Helper method to add the passed role to the roles List.
     */
    public boolean addRole(Role role) {
        return getRoles().add(role);
    }

    /**
     * Helper method to remove the passed role from the roles List.
     */
    public boolean removeRole(Role role) {
        return getRoles().remove(role);
    }

    /**
     * Helper method to determine if the passed role is present in the roles List.
     */
    public boolean containsRole(Role role) {
        return getRoles() != null && getRoles().contains(role);
    }

如果您不介意在您的应用程序中使用 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:

@JoinTable(name = "TOPIC_WRITER", joinColumns = {
        @JoinColumn(name = "topicname", referencedColumnName = "topicname")}, inverseJoinColumns = {
        @JoinColumn(name = "writerid", referencedColumnName = "writerid")})
    @ManyToMany
    private List<Writer> writerList;

@ManyToMany(mappedBy = "writerList")
    private List<Topic> topicList;

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 :

<h:selectManyMenu id="writer" value="#{topicController.selected.writerCollection}" title="#{bundle.CreateBookTitle_writer}" required="true" requiredMessage="Writer needed">
                        <f:selectItems value="#{writerController.itemsAvailableSelectMany}"/>
                    </h:selectManyMenu>

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(){} and removeObject(){} methods in the entity classes concerned by the m2m relationship.

/**
     * Returns the roles List.
     */
    @Cache(usage = NONSTRICT_READ_WRITE)
    @JoinTable(name = "ACCOUNT_ROLE", joinColumns = @JoinColumn(name = "ACCOUNT_ID"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    @ManyToMany(cascade = PERSIST)
    public List<Role> getRoles() {
        return roles;
    }

    /**
     * Set the roles List.
     * It is recommended to use the helper method addRole /  removeRole
     * if you want to preserve referential integrity at the object level.
     *
     * @param roles the List of Role
     */
    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    /**
     * Helper method to add the passed role to the roles List.
     */
    public boolean addRole(Role role) {
        return getRoles().add(role);
    }

    /**
     * Helper method to remove the passed role from the roles List.
     */
    public boolean removeRole(Role role) {
        return getRoles().remove(role);
    }

    /**
     * Helper method to determine if the passed role is present in the roles List.
     */
    public boolean containsRole(Role role) {
        return getRoles() != null && getRoles().contains(role);
    }

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/

水染的天色ゝ2024-11-22 16:17:13

您可以尝试 Netbeans 7.0 中的替代方案,即使用 JSP 作为视图技术而不是 Facelets (XHTML)。如果这样做,IDE 将为托管 Bean 生成不同的代码,并添加一个实用程序类:JsfElResolver.java,它将在每个用户操作时调用,以允许在父级和子级之间导航,就像提交表单一样。
在这种情况下,Many2Many 关系得到部分支持,但如果您的数据库设计良好,它通常会起作用。

正如您所说的插件,在本例中使用 PrimeFacesOpenFaces 作为视图库。这两个库不需要痛苦的配置,并且可以与 Netbeans 中的 JSF CRUD 脚手架完美配合。除了 Primefaces 数据表过滤和排序之外,这在使用 DataModel 时是有问题的。

You can try an alternative in Netbeans 7.0 which is to use JSP as View Technology instead of Facelets (XHTML). If you do so, the IDE will generate a different code for Managed Beans, and add an utility class : JsfElResolver.java which will be called on every user action to allow navigation between parent and child as just as submiting forms.
The Many2Many Relationship is partially supported in this case, but it generally works if your DataBase is well designed.

As what you called Plugin, in this case use PrimeFaces or OpenFaces as A view faces libray. Those two libraries do not need painful configuration and work flawlessly with the JSF CRUD scaffolding in Netbeans. Except Primefaces Datatable filtering and sorting which is buggy when using DataModel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文