-
1. Re: Problem with composite Primary key which is also a forei
fhh Apr 23, 2007 9:34 AM (in response to imranpariyani)Either your pk is ChildPK.class or it is an id. It cannot be both and you can not have two @ids.
Regards
Felix -
2. Re: Problem with composite Primary key which is also a forei
imranpariyani Apr 23, 2007 10:01 AM (in response to imranpariyani)Thanks for the fast reply ...
The primary key for my example is ChildPK.class and according to the ejb3 specification i have the annotation @IdClass(ChildPK.class) in my child class
all the columns in the pk class have to be present in the child class with @Id annotation so there will be two @Id annotations coz of the composite key..
the one you are talking about is the @Embeddable annotation. I am not using that.
Thanks and Regards
Imran -
3. Re: Problem with composite Primary key which is also a forei
oskar.carlstedt Apr 23, 2007 10:53 AM (in response to imranpariyani)Hi!
The @Id annotation will annotate the primary key of your class. The parent id is not the primary key of your class, it is a foreign key, but an @Id in the parent class. Remove @Id from the parentId.
If you want to use composite keys, take a look here http://docs.jboss.org/ejb3/app-server/tutorial/composite/composite.html
Kind Regards
Oskar -
4. Re: Problem with composite Primary key which is also a forei
imranpariyani Apr 23, 2007 11:43 AM (in response to imranpariyani)I think i dint explain my problem that well ... will do it again
CREATE TABLE `parent` (
`id` int(11) NOT NULL auto_increment,
`sitename` varchar(40) NOT NULL,
PRIMARY KEY (`id`))
CREATE TABLE `child` (
`parent_id` int(11) NOT NULL,
`language` enum('EN,'DE','FR') NOT NULL default 'EN',
PRIMARY KEY (`parent_id`,`language`),
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
so as you see the parent_id in the child is a part of composite primary key and also a forigen key ..
so the childPk has the fileds parentId and language ...
and the child class must have the same exact properties as the childPk class and these properties are annotated with multiple @Id annotations..
everything works fine .. like update delete insert .. there is only a prob when i try to insert a new parent .. with the child collection initialized ..
If i first insert the parent with the child collection null and the insert the child collection after assigning the parent object which was inserted then it works fine .. but not if i insert new parent with child objects ... it throws this error of foreign key violation because it tries to insert the child with the foreign key parent id as 0
Regards,
Imran -
5. Re: Problem with composite Primary key which is also a forei
fhh Apr 24, 2007 6:21 AM (in response to imranpariyani)Create a PkClass that has a parent object and a String, like so:
@Embeddable public class Child { private Parent parent; private String language; // Getters and Setters }
-
6. Re: Problem with composite Primary key which is also a forei
mark_man Oct 17, 2008 12:58 AM (in response to imranpariyani)imranpariyani - you have articulated this issue very well. I am having the same problem with JBoss 4.2.2. Did you ever find a solution.
Thanks,
Mark