Q:

Scaffolding ManyToMany associations with Netbeans and JSF

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)

A:

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/

