jQuery in Action, 3rd edition
The moose likes EJB and other Java EE Technologies and the fly likes Compound Keys in EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Compound Keys in EJB" Watch "Compound Keys in EJB" New topic

Compound Keys in EJB


Joined: Aug 13, 2001
Posts: 4
I want to implement compound keys in a CMP bean. Is it sufficient to define the two primary key fields in the ejb-jar.xml file ? How do I use the hashcode methods for creating a record using the compound key.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17421

A compound key is implemented by defining a Primary Key class which contains the two (or more) key fields as public member variables. The exact hashcode algorithm is up to you - you'll want to create an algorithm that gives a good spread over the hash range and has minimal collisions. It's used by the bean server to rapidly determine if the bean is in cache and, if so, return it - it's not used by the create functions except to add the bean to the cache hashtable.
A quick way of creating a compound key EJB is to use the EJBWizard tool at http://www.mousetech.com/EJBWizard.html - on the panel where you define the persistent data, there's a column named "key". Non-key-fields have a key value of 0. Set the first key component to "1", the second to "2", and so forth. It generates a fairly basic generic hash algorithm which should suffice for many cases.
[This message has been edited by Tim Holloway (edited August 14, 2001).]

An IDE is no substitute for an Intelligent Developer.

Joined: Aug 13, 2001
Posts: 4
Thanks for the same. I have already implemented the compound key with the same approach as U have referred to.
I'm facing a different problem right now. I'm able to add data into the database, but the browser throws an error.
"Error 500--Internal Server Error
From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:
10.5.1 500 Internal Server Error
The server encountered an unexpected condition which prevented it from fulfilling the request."
I'm using Weblogic 5.1, SQL 7.0 (sorry for not mentioning it earlier)
I'm using one int variable and the other is String variable(which I have converted into int before using the composite hashcode value)
The problem that is happening is that, the same value of the int variable is being picked again when I try to insert another record with the same variable value set.
Get back, if you could find it.
I agree. Here's the link: http://aspose.com/file-tools
subject: Compound Keys in EJB
It's not a secret anymore!