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 76184 - [55cat] When generating beans, ManyToMany fields are not mapped leading to nonfunction
Summary: [55cat] When generating beans, ManyToMany fields are not mapped leading to no...
Status: VERIFIED FIXED
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 5.x
Hardware: All All
: P2 blocker (vote)
Assignee: Pavel Buzek
URL:
Keywords:
Depends on:
Blocks: 77705
  Show dependency tree
 
Reported: 2006-05-10 22:56 UTC by brviking
Modified: 2006-09-18 11:08 UTC (History)
3 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Sample database diagram (4.44 KB, image/gif)
2006-05-12 02:59 UTC, brviking
Details
Script for sample database diagram (1.05 KB, text/plain)
2006-05-12 02:59 UTC, brviking
Details
Sample project (with correct code) (389.19 KB, application/x-compressed)
2006-05-12 03:01 UTC, brviking
Details
Video showing problem and solution (1.53 MB, application/x-shockwave-flash)
2006-05-12 03:33 UTC, brviking
Details
DERBY db schema (716 bytes, text/plain)
2006-06-05 04:49 UTC, hsalameh
Details
tomcat exception (1.40 KB, text/plain)
2006-06-05 04:50 UTC, hsalameh
Details

Note You need to log in before you can comment on or make changes to this bug.
Description brviking 2006-05-10 22:56:30 UTC
Consider:

create table Persons (PERSON_ID integer primary key, PersonName varchar(100))
create table Users (USER_ID integer primary key, UserName varchar(100))
create table PersonUsers (PERSON_ID integer, USER_ID integer, constraint pkPU
primary key (PERSON_ID, USER_ID)

Now generate Entity Class from Database.

You will see:

    @JoinTable(name = "PersonUsers")
    @ManyToMany
    private java.util.Collection <br.srv.brinfo.beans.User> coUsuario;

where it should be:

    @JoinTable(name = "PersonUsers", joinColumn=@JoinColumn(name="PERSON_ID",
referencedColumnName="PERSON_ID"), inverseJoinColumn=@JoinColumn(name="USER_ID",
referencedColumnName="USER_ID"))
    @ManyToMany
    private java.util.Collection <br.srv.brinfo.beans.User> coUsuario;

(sorry if there is some syntax mistake, I typed everything here instead copy/paste).

Since NetBeans 5.5 knows reference table and reference columns, correct (and
complete) mapping should be generated.

Error messages are not clear enough ("parameter PersonId_PERSON_ID does not
exists") to direct user to correcting the problem.

Tested agains GlassFish b43 and b48 (latest dev build), NB 5.5 Daily
200605050500 and 200605090500. All presents same problem.

Developer could not proceed without figuring out why queries can't executed (and
error message dont help either).
Comment 1 Pavel Buzek 2006-05-11 20:35:44 UTC
This is filed as a duplicate. This is not a P1 issue do I will close this report
and keep the other one. Will fix for 5.5. Thanks.

*** This issue has been marked as a duplicate of 76202 ***
Comment 2 brviking 2006-05-11 21:01:32 UTC
The problem here is not the fact NetBeans is not generating a third class (as
stated in 76202, but the fact that correct column names are not being put by the
wizard. It not only doesn't work, but prevents developer to continue working.
The error message is not enought clear for developer (without deep knoledge of
POJO persistence) to solve and get project working. If it's not P1, at least
it's P2 and is not definitelly duplicate of 76202.
Comment 3 Pavel Buzek 2006-05-11 21:07:16 UTC
I think it is a duplicate. Please read the entire description of 76202 catefully
again.

This is the priority guideline:
http://qa.netbeans.org/bugzilla/bug_priority_guidelines.html
IMO it is P3.
Comment 4 brviking 2006-05-11 22:38:20 UTC
Hi!

Again, the problem reported here is not lack of relation. In fact, when I've
created my beans, relation was created, but only with table name for join table.

My relation table has no additional columns than relation columns. The problem
is because field names follow my own naming pattern, and wizard is expected to
respect this names.

And about priority guidelines, it's a P2:

"Product feature doesn't work, a workaround may exist but it's difficult to use
or impractical"

"Product feature doesn't work": Out of the box, the ManyToMany relation doesn't
work for existing tables.

"A workaround may exists, but it's difficult to use or impratical": since error
message thrown by Application Server during runtime reports a invalid "column
name" that could not even can be found (throught Find... in project), is really
very difficult to fix it. The reported column name could not be find in any XML
file, neither in Java code. So, or developer goes deep inside defaults assumed
for particular AS implementation (in this case, it's Glassfish, but it could be
other), or the wizard don't goes into defaults for this kind of mapping (so
explicit mapping of relation columns should be obligatory).

Difference from 76202 is because I have no additional columns in relation table,
neither mapping was missed. The mapping is present, but it's wrong. If you wish
to add this issue to 76202, well, this is your choice, but add clearly that
problem is lack of "@JoinColumn" for joinColumns and inverseJoinColumns
Example of code generated by wizard (so the relation was generated, in contrary
to what is reported in 76202):

    @JoinTable(name = "RL_USUARIO_PESSOA")
    @ManyToMany
    private java.util.Collection <sgeejb.beans.TbUsuario> coUsuario;

Expected code from wizard:

    @JoinTable(name = "RL_USUARIO_PESSOA",
inverseJoinColumns=@JoinColumn(name="CO_USUARIO",
referencedColumnName="CO_USUARIO"), joinColumns=@JoinColumn(name="CO_PESSOA",
referencedColumnName="CO_PESSOA"))
    @ManyToMany
    private java.util.Collection <br.srv.brinfo.beans.TbUsuario> coUsuario;


I think this make things clear!

If you wish, transfer this information to 76202, but raise 76202 to P2 and copy
this info into them. Without fixing it, ManyToMany with existing tables doesn't
work out-of-the-box, neither is easy to fix (specially if developer isn't expert
in persistence - target for Wizards).
Comment 5 Pavel Buzek 2006-05-11 23:55:31 UTC
Is the DB script above complete? Looks like it creates 3 tables but I do not see
where is the definition of foreign keys. I just want to make sure that I can
reproduce.
Comment 6 Pavel Buzek 2006-05-12 00:10:14 UTC
There are similar issues, like issue 74117 or issue 76202 which are tracked as P3.
It was a consensus of the team working on this for issue 74177 to track this as
a P3. We will fix it for 5.5, do not worry.

I will look at whether it is a duplicate or not when I can test with the
complete SQL script.
Comment 7 brviking 2006-05-12 02:59:20 UTC
Created attachment 30356 [details]
Sample database diagram
Comment 8 brviking 2006-05-12 02:59:50 UTC
Created attachment 30357 [details]
Script for sample database diagram
Comment 9 brviking 2006-05-12 03:01:36 UTC
Created attachment 30358 [details]
Sample project (with correct code)
Comment 10 brviking 2006-05-12 03:33:20 UTC
Created attachment 30359 [details]
Video showing problem and solution
Comment 11 Pavel Buzek 2006-05-20 00:48:40 UTC
Richter is right, it is not a duplicate of issue 76202. Reopening.
Comment 12 brviking 2006-05-22 17:30:53 UTC
Changing to [55cat] to be conformant with NetCAT program.
Comment 13 Pavel Buzek 2006-06-04 22:42:16 UTC
Checking in
src/org/netbeans/modules/j2ee/persistence/entitygenerator/CMPMappingModel.java;
/cvs/j2ee/persistence/src/org/netbeans/modules/j2ee/persistence/entitygenerator/Attic/CMPMappingModel.java,v
 <--  CMPMappingModel.java
new revision: 1.1.2.1.2.1; previous revision: 1.1.2.1
done
Checking in
src/org/netbeans/modules/j2ee/persistence/entitygenerator/DbSchemaEjbGenerator.java;
/cvs/j2ee/persistence/src/org/netbeans/modules/j2ee/persistence/entitygenerator/Attic/DbSchemaEjbGenerator.java,v
 <--  DbSchemaEjbGenerator.
java
new revision: 1.1.2.1.2.6; previous revision: 1.1.2.1.2.5
done
Checking in
src/org/netbeans/modules/j2ee/persistence/wizard/fromdb/JavaPersistenceGenerator.java;
/cvs/j2ee/persistence/src/org/netbeans/modules/j2ee/persistence/wizard/fromdb/Attic/JavaPersistenceGenerator.java,v
 <--  JavaPersistenceGene
rator.java
new revision: 1.1.2.2.2.26; previous revision: 1.1.2.2.2.25
done
Comment 14 hsalameh 2006-06-05 04:48:52 UTC
Something still not right.
I checked out from cvs, and made a local build.
I tired with the simple example of three tables, A, B, and MAP_A_B, and it gave
a tomcat exception when trying to associate one entity with the other.
I will attach the database schema and the exception below.

After creating the entities from the database, and the jsf from entities, access
the pages, create a new A, create a new B, and then go to the list of As, access
the details of the A instance and try to add a new B to it.
This will give a tomcat exception as the one show in the attachement below.
Comment 15 hsalameh 2006-06-05 04:49:44 UTC
Created attachment 30777 [details]
DERBY db schema
Comment 16 hsalameh 2006-06-05 04:50:27 UTC
Created attachment 30778 [details]
tomcat exception
Comment 17 Pavel Buzek 2006-06-05 14:09:10 UTC
This is a different problem again, but let's keep it here since this is our
favorite bug number :-)

In the ManyToMany case the controller does not have helper methods for
new/destroy of related objects from a detail page of entity. This works ok only
for the 1:n relationship. I will look at that (as time permits).
Comment 18 brviking 2006-06-07 12:46:38 UTC
There is an additional issue.
Where exists a composed primary key, the @JoinColumn references must
(obligatory) name="", referencedColumnName="" (look at specs, or Right Clic ->
Verify Project).
Actual implementation just put "@JoinColumn(name="...")", and should be
@JoinColumn(name="...", referencedColumnName="...").
Comment 19 Pavel Buzek 2006-06-26 20:07:24 UTC
"brviking", please do not pile any more issues in this report. I will fix the
last issue reported by Halim, for any other problems please file a separate issue.
Also, for that other issue: I do not understand what exactly you mean. AFAICT it
generates both name and referencedColumnName in JoinColumn annotation. Is that
not true?
Comment 20 Pavel Buzek 2006-06-26 22:07:21 UTC
/cvs/web/jsf/src/org/netbeans/modules/web/jsf/palette/items/Attic/JsfForm.java,v
 new revision: 1.1.2.4.2.18; previous revision: 1.1.2.4.2.17
/cvs/web/jsf/src/org/netbeans/modules/web/jsf/wizards/Attic/JSFClinetGenerator.java,v
 new revision: 1.1.2.24; previous revision: 1.1.2.23

hopefully I fixed the controller for many to many
Comment 21 brviking 2006-06-27 02:19:43 UTC
Sorry. I didn't want to be boring anyone... Just reporting the previous solution
was incomplete.
About the my many to many last report the answer is yes, there is missing
referencedColumnNamnes, but I didn't tested in later daily builds. I was very
busy here.
I'll test again in latest daily build.
Tkx.
Comment 22 hsalameh 2006-06-27 04:37:19 UTC
Pavel:
I just updated form CVS and made a local build to test this new fix.
I am a little confused now.
I was expecting it to work in a different way that how it works now.

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.

Comment 23 Pavel Buzek 2006-06-27 05:05:29 UTC
Richter: I did not mean it bad, I just think this issue is getting a little bit
confusing and a bit long. Please test it and report a new defect it you find
that it does not work.

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.
Comment 24 hsalameh 2006-06-27 05:15:23 UTC
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.

 
Comment 25 hsalameh 2006-06-27 16:55:30 UTC
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 26 Pavel Buzek 2006-06-27 18:57:52 UTC
I filed 79243 for the support to add items into many to many. First because that
one is not a P2, IMO, and second because the original problem (and a few more)
was resolved.
Comment 27 Jan Horvath 2006-09-18 11:08:28 UTC
v