-
1. Re: How to map composite primary key?
dsundstrom Aug 13, 2002 5:37 PM (in response to tdang)You need a custom primary key class. I suggest you take a look at one of the EJB books; look for primary key class in the index.
-
2. Re: How to map composite primary key?
gummy Aug 30, 2002 8:53 PM (in response to tdang)Hi Dain,
I did use the primary key class I created, and I declared in the ejb-jar.xml file under that particular
entity as:
.
.
<prim-key-class>table1Key</prim-key-class>
.
.
but I am still getting an error when I started the server.
javax.ejb.ObjectNotFoundException: Object with primary key table1Key@4df1cf5b not found in storage
Do you know what I might do wrong?
David Li.
ps. thx for the comments on my other post, I am trying to read the docments on JBoss3.0.1 now. and in the progress of testing xdoclet to generate my ejb deployment scripts. -
3. Re: How to map composite primary key?
gummy Aug 30, 2002 8:54 PM (in response to tdang)Hi Dain,
I did use the primary key class I created, and I declared in the ejb-jar.xml file under that particular
entity as:
.
.
<prim-key-class>table1Key</prim-key-class>
.
.
but I am still getting an error when I started the server.
javax.ejb.ObjectNotFoundException: Object with primary key table1Key@4df1cf5b not found in storage
Do you know what I might do wrong?
David Li.
ps. thx for the comments on my other post, I am trying to read the docments on JBoss3.0.1 now. and in the progress of testing xdoclet to generate my ejb deployment scripts. -
4. Re: How to map composite primary key?
dsundstrom Sep 2, 2002 12:56 PM (in response to tdang)You could be doing lots of things wrong. It is very hard to get the primary key class correct (a lot of generators like JBuilder get it very wrong).
The two most important methods are equals and hashCode. Don't try to implement any inheritance in your pk classes; it is way more trouble then it is worth.
Here's some sample code. This may not be perfect; I writing it from memory: [pre]
public boolean equals(Object o) {
if(!(o instanceof MyPkType)) {
return false;
}
// will never be null
MyPkType myPk = (MyPkType)o;
return myPk.blah1.equals(this.blah1) &&
myPk.blah2.equals(this.blah2) &&
myPk.blah3.equals(this.blah3) &&
myPk.blah4.equals(this.blah4);
}
public int hashCode() {
int result = 17;
result = 37*result + (blah1==null ? 0 : blah1.hashCode());
result = 37*result + (blah2==null ? 0 : blah2.hashCode());
result = 37*result + (blah3==null ? 0 : blah3.hashCode());
result = 37*result + (blah4==null ? 0 : blah4.hashCode());
return result;
} [/pre] -
5. Re: How to map composite primary key?
dokomesiter Sep 14, 2002 6:20 PM (in response to tdang)Might I suggest that it would be enormously helpful if the JBossCMP book simply changed one of the tables, such as gangster, from primitive key (integer gangsterId), to a composite primary key (GangsterKey{firstName,lastName}).
It seems there are many of us who have been beating our heads for many hours (or at least I have mine) on the vast multitude of ripple affects from such a seemingly "trivial" change for such a basic design pattern.
Stump point #1: primkey
primkey-field, primkey-class mismatch
(primkey omission)
Stump point #2: CMR
how to map CMR column name to multiple fields
Stump point #3: create
table generation failure
(turn off primary key constraint)
Stump point #4: exists
JBoss omits where: SELECT COUNT(*) FROM TBL WHERE
(still working on this one)
Stump point #5: DVC
etc. etc.
Cheers,
Chris -
6. Re: How to map composite primary key?
dsundstrom Sep 17, 2002 3:09 PM (in response to tdang)I'm actually planning on writing a short article on the correct way to write a compoung primary key.