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)
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/