This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 79243 - support to add items into Many2Many relationship
Summary: support to add items into Many2Many relationship
Status: RESOLVED FIXED
Alias: None
Product: javaee
Classification: Unclassified
Component: JSF (show other bugs)
Version: 5.x
Hardware: PC Windows XP
: P3 blocker with 1 vote (vote)
Assignee: Pavel Buzek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-06-27 18:55 UTC by Pavel Buzek
Modified: 2006-08-10 23:44 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
build error (3.24 KB, text/plain)
2006-08-09 07:23 UTC, hsalameh
Details
NB project (60.62 KB, application/octet-stream)
2006-08-09 07:24 UTC, hsalameh
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pavel Buzek 2006-06-27 18:55:59 UTC
Here is the relevant part of 76184:

Halim:

Let's say we have a Student and Course tables related in a many-to-many 
relation.
Now if i create 10 students, and then I create 10 courses. Now I go to the list 
of courses, click on the ID of a course to open its details. Now there is no 
way to associate any of the students that I created earlier with this course.
The only option I have is to create new students that will be associated with 
this course.

The way it works now has really no practical use.
 
The way I thought it should work is that once you open the detail of a Course, 
you will have the opportunity to add any number of the existing students to 
that course.
I think this would be the natural way that a usere would want to use an 
application that has a many-to-many association.


Please let me know if you agree.
I am reopening this report for now. If you don't agree with me, you can change 
it to the appropriate status.
thanks.

------- Additional comments from pbuzek Tue Jun 27 04:05:29 +0000 2006 -------

Halim: What you are desribing makes sense and would be a good thing to add. I
would like to minimize the amount of new code though and focus only on fixing
existing issues. The problem is that I would need to figure out what exactly it
should look like in the first place..... 

Do you mean that creating new Students from a detail of Course makes no sense?
Maybe I should keep the "New Student" link and just add some minimal UI to add
an existing Student. One way to do it would be to have a combo box with Student
and an Add button with Students, so you can at least add one at a time. For
adding multiple I would probably need to create a whole new page for selection
of Students which sounds like too much work at this stage.

Let me know if you have any ideas. Thanks.

------- Additional comments from hsalameh Tue Jun 27 04:15:23 +0000 2006 -------

Pavel:

You are absolutely right.
Adding multiple Students to a Course in one shot would require a whole new 
page. If this is not an option at this time (which I perfectly understand), 
your suggestion of having a combo box that allows adding one student at a time 
would be GREAT! 
As for the link that you have now for adding a new student, i don't see this 
necessary, but it probably won't hurt. So if you want to keep it, it is your 
call. One thing that would be nice though is that after you create this new 
student, it will bring you back to the *course* page and not back to the 
students list. This will probably require changes in the "new student" page 
becase it will have two destinations now depending on where it was called from.

Anyway... If you want to choose only one thing to do, I would vote for the 
combo to allow adding an existing student to a course.
Thanks for your great effort.

 

------- Additional comments from hsalameh Tue Jun 27 15:55:30 +0000 2006 -------

Actually, after a little more thinking, I take back what I said about the "New
Student" being unnecessary. I can see where it can come in handy in some
situations. So I agree with what you said about that.

So it would be great if you can keep it and just add the combo to enable the
addition of a single existing "Student".
Comment 1 Pavel Buzek 2006-06-27 18:56:22 UTC
will try to fix after beta 2
Comment 2 hsalameh 2006-08-04 16:53:06 UTC
This request is the same as the one mentioned on the forum with a subject
"Generated JSF and many-to-many relationships"  by "William T. Mann" dated
7/27/2006.

Is there a plan to get this done for 5.5?
I think this is a very important issue that will make the generated JSF
application useable in real situations.
Comment 4 hsalameh 2006-08-09 07:22:30 UTC
Pavel,
I just updated from CVS and made a local build to test this issue.
After generating the entities and JSF pages, i got a compile error when i tried
to compile the project.
I am attaching below two files:

build error.txt : shows the build error that i get in NB.

WebApplication4.zip : contains the NB project

The DB is derby and there are four tables
CONTRACT, CUSTOMER, VEHICLE, CONTRACT_CUSTOMER.
they are related to each other as follows:
one contract to many vehicles
many contracts to many customers (using the join table)

please let me know if you need more information.
Comment 5 hsalameh 2006-08-09 07:23:20 UTC
Created attachment 32678 [details]
build error
Comment 6 hsalameh 2006-08-09 07:24:09 UTC
Created attachment 32679 [details]
NB project
Comment 7 hsalameh 2006-08-09 07:28:18 UTC
Sorry, I made a mistake. The tables are related to each other as follows:
one vehicle to many contracts
many contracts to many customers (using the join table)
Comment 8 hsalameh 2006-08-09 10:10:28 UTC
Here is a simpler schema to try it with. Just create a derby database with this 
schema, and use the wizards to generate the entities and JSF pages, and see how 
it won't compile.

CREATE TABLE STUDENT 
    (
    ID          INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    NAME        VARCHAR(255),
    DESCRIPTION VARCHAR(255)
    );

CREATE TABLE COURSE
    (
    ID          INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    NAME        VARCHAR(255),
    DESCRIPTION VARCHAR(255),
    TEACHER_ID  INTEGER
    );

CREATE TABLE TEACHER
    (
    ID          INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    NAME        VARCHAR(255),
    DESCRIPTION VARCHAR(255)
    );

            
CREATE TABLE STUDENT_COURSE 
    (
    STUDENT_ID               INTEGER NOT NULL,
    COURSE_ID               INTEGER NOT NULL
    );

ALTER TABLE COURSE
    ADD CONSTRAINT  FK_COURSE__TEACHER_ID
    FOREIGN KEY     (TEACHER_ID)
    REFERENCES      TEACHER (ID);
                
ALTER TABLE STUDENT_COURSE
    ADD CONSTRAINT  FK_STUDENT_COURSE__STUDENT_ID
    FOREIGN KEY     (STUDENT_ID)
    REFERENCES      STUDENT (ID);

ALTER TABLE STUDENT_COURSE
    ADD CONSTRAINT  FK_STUDENT_COURSE__COURSE_ID
    FOREIGN KEY     (COURSE_ID)
    REFERENCES      COURSE (ID);
Comment 9 Pavel Buzek 2006-08-09 18:41:37 UTC
I forgot to test with a table witch as M:N and also 1:N, the 1:N was causing the
problem. Will fix this promptly, thanks for testing.
Comment 10 hsalameh 2006-08-09 18:47:45 UTC
Pavel, 
I also tried it with a student-course database, (without the teacher table).
In this case, it generates the code, and compiles fine, but then when I tried 
to add an existing course to a student, it gave a runtime exception.
I hope you can test this scenario as well... just use this last schema, but 
remove the teacher table.
thanks

Comment 11 hsalameh 2006-08-10 05:35:57 UTC
Here is the runtime exception that I get if the DB contained only M:N and no 
1:N. I am using the above COURSE - STUDENT example (without the TEACHER table)
This exception happens when I try to add a student to a course:


HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from 
fulfilling this request.

exception

javax.servlet.ServletException: #{course.studentIdCollection}: 
javax.faces.el.MethodNotFoundException: studentIdCollection: 
test.CourseController.studentIdCollection()
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:209)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter
(MonitorFilter.java:368)

root cause

javax.faces.FacesException: #{course.studentIdCollection}: 
javax.faces.el.MethodNotFoundException: studentIdCollection: 
test.CourseController.studentIdCollection()
	com.sun.faces.application.ActionListenerImpl.processAction
(ActionListenerImpl.java:74)
	javax.faces.component.UICommand.broadcast(UICommand.java:312)
	javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
	javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
	com.sun.faces.lifecycle.InvokeApplicationPhase.execute
(InvokeApplicationPhase.java:75)
	com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
	com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter
(MonitorFilter.java:368)

note The full stack trace of the root cause is available in the Apache 
Tomcat/5.5.17 logs.
Apache Tomcat/5.5.17
Comment 12 Pavel Buzek 2006-08-10 19:45:01 UTC
Halim, thanks for testing. I think I fixed both issues. Please try again and let
me know. BTW: we did not talk about "Remove" but I added that for M:N as well as
add. Let me know how you like the UI. Thanks for you patience :-)

http://web.netbeans.org/source/browse/web/jsf/src/org/netbeans/modules/web/jsf/palette/items/JsfForm.java?r1=1.1.2.4.2.22&r2=1.1.2.4.2.23
http://web.netbeans.org/source/browse/web/jsf/src/org/netbeans/modules/web/jsf/wizards/JSFClinetGenerator.java?r1=1.1.2.39&r2=1.1.2.40
Comment 13 hsalameh 2006-08-10 21:56:30 UTC
Brilliant.
Thank you so very much Pavel.
This works great!

There are two minor issues I still have regarding these wizards. I am going to 
state them here, and let me know if I should create separate tickets for them.

1) when I create the entities from database using the wizard, then open the JSF 
wizard, I don't see the entities in the list to choose from. To solve this, I 
discovered, I just open the persistence.xml and press save, and then go back to 
the JSF wizard. At this point, the entities will appear. There must be 
something wrong in creating the entities from database wizard such that it does 
not update the persistence.xml immediately. Ofcourse you know better about 
this. If you want me to submit a separate issue, let me know.

2) in the generated pages, if you open the detail of one of the M:N items for 
example the COURSE, you will see the "New StudentIdCollection ". I think this 
should be named "New StudentId" without the collection, because we're createing 
a single student and not a collection. In addition, after you click it, and 
create a new student, it takes you back to the student list. I think the normal 
thing is to bring you back to the Course detail page because that's where you 
were before creating this new student.

I will keep this ticket as resolved because it is really resolved. Please let 
me know if you want me to create separate tickets for these two issues.

Again, thank you for your great work. I am really happy that this functionality 
is implemented now! Thank you.
Comment 14 Pavel Buzek 2006-08-10 22:25:39 UTC
Halim, glad you like it. Thanks for testing.

The first issue: are you sure you need to save the persistence.xml? More likely
you just need to open the wizard again to see the entity classes. This has been
filed and suposedly resolved but I saw it myself several times (not 100%
reproducible) recently. So it's is a known issue.

Second problem: this is the result of how we generate the field names in entity
classes. JSF from entity just uses the field names. Maybe we could allow
customization of these field names when you generate entity from DB, but that is
a separate issue for future (you can file enhancement and vote :-). I think in
reality you will probably want to edit the labels anyway so I do not think it is
very useful to implement some complex heuristic to try to generate nicer names.
Remember that if you write entities by hand they will not follow the convention
with "collection". I would leave it as it is.
Comment 15 hsalameh 2006-08-10 22:35:06 UTC
Pavel:
The first issue is reproducible for me every time!
After the "Entities from Database" wizard finishes its work, I open the "JSF 
pages from entities" wizard and there is nothing in the list. So i close it and 
open it again, and still, nothing there, so i close it again. Then I open the 
persistence.xml and press ^S to save (as simple as that). Then I open the JSF 
wizard again, and the entities appear in the wizard now! I hope this helps 
identifying the problem.


As for the second issue, I agree with what you said. But it had two parts. You 
forgot to comment on the second part which is: 
"In addition, after you click it, and 
create a new student, it takes you back to the student list. I think the normal 
thing is to bring you back to the Course detail page because that's where you 
were before creating this new student."

Thanks.
Comment 16 hsalameh 2006-08-10 23:10:26 UTC
Pavel, 
Actually I guess I need to reopen this issue because I found related problem.
Here is what you can do to reproduce (please use the Student-Course-Teacher 
schema previously attached above).
1) after creating the JSF pages, run the application and open the Student list. 
create couple of students.
2) open the course list and create couple of courses.
3) open the detail of a course that you created in step 2 and add to it one of 
the students that you created in step 1.
4) close the browser and open a new browser to start a new session.
5) open the course list page and try to open the detail of the course
6) you will get the exception attached at the end.
7) go back and open the student list page, and open the details of a student, 
it will work
8) go back to the course list and open the destils of that same course, and it 
will work this time.

here is the exception that you will get in step 6:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from 
fulfilling this request.

exception

javax.servlet.ServletException: #{course.detailSetup}: 
javax.faces.el.EvaluationException: java.lang.NullPointerException
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:209)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter
(MonitorFilter.java:368)

root cause

javax.faces.FacesException: #{course.detailSetup}: 
javax.faces.el.EvaluationException: java.lang.NullPointerException
	com.sun.faces.application.ActionListenerImpl.processAction
(ActionListenerImpl.java:78)
	javax.faces.component.UICommand.broadcast(UICommand.java:312)
	javax.faces.component.UIData.broadcast(UIData.java:657)
	javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
	javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
	com.sun.faces.lifecycle.InvokeApplicationPhase.execute
(InvokeApplicationPhase.java:75)
	com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
	com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter
(MonitorFilter.java:368)

note The full stack trace of the root cause is available in the Apache 
Tomcat/5.5.17 logs.
Apache Tomcat/5.5.17
Comment 17 Pavel Buzek 2006-08-10 23:44:29 UTC
The last issue is not related to adding m:n, IMO. In any case it is a different
problem. Please file a new bug.

I forgot about the navigation issues. This is also not related just to m:n, same
problem exists for 1:n. Can you also file a P3 defect, web/jsf, assign to me? 

or email me and i will file it myself. thanks