NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
mn1247
Joined: 04 Jan 2011 Posts: 64
|
Posted: Fri Jan 28, 2011 3:51 pm Post subject: Generating entity classes from database - question |
|
|
I have a question about entity beans. Suppose I have two tables that have a one-to-many relationship. Let's say, Table A is on the "one" side and Table B is on the "many" side.
If I use the NB "Entities from database" wizard, I seem to only get a bean generated for Table A. Table B does not result in an entity class, and the wizard just says "no primary key" when I try to select it for inclusion.
Is this how it's supposed to be? Should only Table A get an entity class? Or is something going wrong?
Thanks
Eric |
|
| Back to top |
|
 |
Antonio Varela Posted via mailing list.
|
Posted: Fri Jan 28, 2011 9:17 pm Post subject: Re: Generating entity classes from database - question |
|
|
On Fri, Jan 28, 2011 at 8:51 AM, mn1247 <address-removed> wrote:
| Quote: | | I have a question about entity beans. |
|
| Back to top |
|
 |
edwin
Joined: 17 Jan 2011 Posts: 7
|
Posted: Sat Jan 29, 2011 7:54 am Post subject: |
|
|
Hi Eric,
I believe both tables need to have a primary key defined. In addition the foreign key constraint should be defined in the database so that the related table automatically gets picked up. I just tested it and it worked for me.
Composite keys are also possible but I am not sure if this works with the wizard. In any case the primary key should be defined. |
|
| Back to top |
|
 |
mn1247
Joined: 04 Jan 2011 Posts: 64
|
Posted: Sat Jan 29, 2011 2:38 pm Post subject: |
|
|
Thanks for the response. Here's an example (simplified for clarity) of the tables I'm using. They both seem to have primary keys.
Where am I going wrong?
Eric
The "one" tableā¦.
CREATE TABLE `tblaxis1` (
`DSMCodeID` mediumint(9) DEFAULT NULL,
`PKID` mediumint(9) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`PKID`),
KEY `DSMCodeID` (`DSMCodeID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The "many" table...
CREATE TABLE `lstdsmcodes` (
`PKID` mediumint(9) NOT NULL AUTO_INCREMENT,
`DSMCodesID` mediumint(9) NOT NULL,
PRIMARY KEY (`PKID`),
KEY `DSMCodesID` (`DSMCodesID`),
CONSTRAINT `lstdsmcodes_ibfk_1` FOREIGN KEY (`DSMCodesID`) REFERENCES `tblaxis1` (`DSMCodeID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
|
| Back to top |
|
 |
edwin
Joined: 17 Jan 2011 Posts: 7
|
Posted: Sat Jan 29, 2011 7:43 pm Post subject: |
|
|
I tried it with your tables as well and both entity classes were generated by the wizard so there must be something wrong with your installation. I use 6.9.1 as well.
On a side note, I think you foreign key does not make sense. It is not a one to many relationship as it does not refer to the primary key in the other table. A typical one to many relationship would be the following where the country_id in the person table refers to the id of the country table.
PERSON
id (PK)
name
country_id (FK)
COUNTRY
id (PK)
name |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|
|