Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate and Composite Foreign Keys

 
Carlos Alfonso Ortiz Rodriguez
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

I was playing around with Hibernate for a little while. I'm just a rookie.
Everything went ok until I got into the need for some very basic but specific thing to be done that i couldn't ... so I'm asking you guys if it is true what I read in some forums that this behaviour is not fully supported by hibernate right now.

Here is the thing.
Suppose I have the following tables:

CREATE TABLE `proyect` (
`proyectId` int(10) unsigned NOT NULL,
`name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`proyectId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `color` (
`proyectId` int(10) unsigned NOT NULL,
`colorId` int(10) unsigned NOT NULL,
`name` varchar(30) DEFAULT NULL,
PRIMARY KEY (`proyectId`,`colorId`),
CONSTRAINT `color_ibfk_1` FOREIGN KEY (`proyectId`) REFERENCES `proyect` (`proyectId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `piece` (
`proyectId` int(10) unsigned NOT NULL,
`pieceId` int(10) unsigned NOT NULL,
`colorId` int(10) unsigned DEFAULT NULL,
`name` varchar(30) DEFAULT NULL,
PRIMARY KEY (`proyectId`,`pieceId`),
KEY `proyectId` (`proyectId`,`colorId`),
CONSTRAINT `piece_ibfk_1` FOREIGN KEY (`proyectId`) REFERENCES `proyect` (`proyectId`),
CONSTRAINT `piece_ibfk_2` FOREIGN KEY (`proyectId`, `colorId`) REFERENCES `color` (`proyectId`, `colorId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

So there are proyects that can have pieces. There are color catalogues for each proyect, and the pieces of each proyect may or may not have a color.
Please note the foreign keys that are in the color and piece table, especially in the piece table, because is a composite one, that ensures that pieces of a certain proyect can have only colors that belong to that proyect's catalog.

The point is, i did everything to get to the point: session.save(piece) and attributes are updated well.. but when i change the id of the color to another or just set the id to null is not working. No error, just not working.

So, does Hibernate is prepared to handle this situation?

Thanks in advance!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic