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 80084 - Entity Classes from DB: mappings for a PK whose part is also a FK
Summary: Entity Classes from DB: mappings for a PK whose part is also a FK
Status: RESOLVED FIXED
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 5.x
Hardware: All All
: P3 blocker (vote)
Assignee: Andrei Badea
URL:
Keywords:
: 82382 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-07-12 16:11 UTC by Petr Blaha
Modified: 2006-08-14 15:49 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
stack trace (1.94 KB, application/octet-stream)
2006-07-12 16:13 UTC, Petr Blaha
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Petr Blaha 2006-07-12 16:11:18 UTC
[build 20060711 Beta2]
I got java.io.IOException: Unexpected error while generating the entity class
during generation of entity classes in web project from MySQL database. The full
stack trace is attached.
Comment 1 Petr Blaha 2006-07-12 16:13:12 UTC
Created attachment 31773 [details]
stack trace
Comment 2 Petr Blaha 2006-07-12 16:29:04 UTC
I am able to reproduce this issue with this table only:

CREATE TABLE `ACCOUNT` (
  `ID_ACCOUNT` int(11) NOT NULL default '0',
  `MESTO` char(1) NOT NULL default '',
  `ID_UZIVATEL` int(11) NOT NULL default '0',
  `AMOUNT` float default NULL,
  PRIMARY KEY  (`ID_ACCOUNT`,`MESTO`),
  KEY `ID_UZIVATEL` (`ID_UZIVATEL`),
  KEY `MESTO` (`MESTO`),
  CONSTRAINT `ACCOUNT_ibfk_1` FOREIGN KEY (`ID_UZIVATEL`) REFERENCES `UZIVATEL`
(`ID`),
  CONSTRAINT `ACCOUNT_ibfk_2` FOREIGN KEY (`MESTO`) REFERENCES `MESTO` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Comment 3 Rochelle Raccah 2006-07-12 19:08:33 UTC
The NPE is at the line that contained a hotfix and was removed when issue 74117
was fixed.
Comment 4 Andrei Badea 2006-07-12 19:28:09 UTC
Didn't look at it yet, but it is probably different from 74117 (ACCOUNT does not
reference itself). I would say it has something to do with a part of the primary
key being also a foreign key. But just wildly guessing, I will look at it tomorrow.
Comment 5 Rochelle Raccah 2006-07-12 19:57:29 UTC
I'm not saying it's the same bug, just that the null check for finding any 
columns that was the hotfix for that bug also prevented the NPE here.
Comment 6 Andrei Badea 2006-07-13 17:16:12 UTC
It is indeed caused by a part of the composite primary key (MESTO) being also a
foreign key. Both a CMP and a CMR field are generated in the maping model for
the MESTO column and the uniquing algorithm causes the CMR field to be renamed
to mesto-1, for which there are of course no mappings. The mapping model tries
to work around this situation (see DbSchemaEjbGenerator.java:355), but it
probably fails when the name of the foreign key column (here "MESTO") is the
same as the name of the referenced table ("MESTO"). An approach to fix this
would be to detect the conflict and generate another name for the CMR field,
like "mestoRef" or something.

The workaround is to use a different name than the referenced table name for the
foreign key column name. 

When I applied the workaround the generation process didn't fail, but a a
setMesto() method was generated in Account which allows changing the primary key
value. But in 2.1.4. the JPA spec says the application should not change the
primary key value. Should we then not generate the setter for mesto? I think the
situation is similar in EJB 2.1, isn't it? 

Should be fixed for 5.5.

Pavle, Rochelle, please comment.
Comment 7 Pavel Buzek 2006-07-13 18:15:40 UTC
Can you attach the full DB script for Java DB so that it would be easily
reproducible? I tried to create a script with part of PK being a foreign key but
entity generation works OK. What did I miss?

DROP TABLE "ACCOUNT";  DROP TABLE "UZIVATEL";

CREATE TABLE "UZIVATEL" ( "ID" int not null,  "JMENO" varchar(40),  PRIMARY KEY
("ID"));

CREATE TABLE "ACCOUNT" (  "ID_ACCOUNT" int NOT NULL, "ID_UZIVATEL" int NOT NULL,
  "AMOUNT" float default NULL,   PRIMARY KEY  ("ID_ACCOUNT","ID_UZIVATEL"),
  CONSTRAINT "ACCOUNT_ibfk_1" FOREIGN KEY ("ID_UZIVATEL") REFERENCES "UZIVATEL"
("ID")
);
Comment 8 Pavel Buzek 2006-07-13 18:24:18 UTC
sorry, I was not reading carefully, I got it now. The script for Java DB:

DROP TABLE "ACCOUNT"; DROP TABLE "UZIVATEL";

CREATE TABLE "UZIVATEL" (  "ID" int not null,   "JMENO" varchar(40), 
  PRIMARY KEY ("ID") );

CREATE TABLE "ACCOUNT" (   "ID_ACCOUNT" int NOT NULL,   "UZIVATEL" int NOT NULL,
  "AMOUNT" float default NULL,   PRIMARY KEY  ("ID_ACCOUNT","UZIVATEL"),
  CONSTRAINT "ACCOUNT_ibfk_1" FOREIGN KEY ("UZIVATEL") REFERENCES "UZIVATEL" ("ID")
);
Comment 9 Rochelle Raccah 2006-07-13 23:23:11 UTC
We currently (also) generate public setter methods for pk fields when it's not
overlapping with a fk.  When I asked about whether we should generate those with
a different modifier, we decided not to because of the different id/generator
strategies.  So, I'm not sure how generation of a setter is different in this
case...

I'll ask for more input from the rest of the persistence team.

Please also see issue 77730 which is related to overlapping pk/fk.
Comment 10 Rochelle Raccah 2006-07-17 23:46:24 UTC
Marina confirmed that we should not worry about skipping the set method
generated that affects the pk. This is because there are no managed
relationships in Java Persistence spec, and that requires the user to set it on
all sides.
Comment 11 Andrei Badea 2006-08-10 13:30:32 UTC
*** Issue 82382 has been marked as a duplicate of this issue. ***
Comment 12 Andrei Badea 2006-08-14 15:48:48 UTC
When renaming a field during the uniquifying process, its mapping in
CMPMappingModel.getCmrFieldMapping() should be updated accordingly, which
currently it is not. This will cause a getUzivatel1() method to be generated for
the schema in this issue, which is not 100% perfect, but at least does not cause
the generation process to fail.
Comment 13 Andrei Badea 2006-08-14 15:49:36 UTC
Fixed.

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.11; previous revision: 1.1.2.1.2.10
done