|
Automatic generation of unique primary keys This article describes how to automatically generate unique primary keys for your EJB Entities. 1 Introduction Sometimes it is useful to have primary keys that holds no information of value besides making the Entity unique. Many different techniques exist to automate this process. This article will describe some of these techniques and try to present the pro and cons of using them. 2 Counter utility Automatic generation of unique primary keys can be performed by using a counter utility that keeps track of the current highest value of used primary key. When a new value is requested the counter utility increases the current highest value. One such counter utility is the counter.jar EJB-module produced by IronFlare. This EJB-module can be included in the users application and then referenced when a unique long value is needed. In order to be as effective as possible, the EJB-module gets a series of values to hold on to during its lifetime and to handle out on request. When these values are all used up, the EJB-module gets a new series of unique values. This limits the number of requests against the underlying DataSource to a minimum.
The drawback of using such a counter utility is that they have a limitation on the number of unique keys they can produce. On the other hand, referencing the key is very easy. 3 Container generated It is required by the EJB specification that the Container should be able to automatically generate unique primary keys using Object as key for CMP Entity beans. This is very useful when you don't need to show the primary key to the client or have the client select a primary key. In order for the Container to generate primary keys for an CMP Entity the following is required: The ejbCreate method should have a return type of java.lang.Object and return null. The <persistence-type> tag should contain a value of Container in the ejb-jar.xml entry for the entity The <prim-key-class> tag should contain a value of java.lang.Object in the ejb-jar.xml entry for the entity The <primkey-field> should not be given in the ejb-jar.xml entry for the entity Using this approach to generate unique primary keys is very nice as long as you don't have to reference the primary key itself, as the underlying type represented by Object is decided by the Container at deployment time. This also means that you should not have any implementation methods that use the primary key or rely on methods that have to reference it.
For more information please see the ATM J2EE application sample shipped with Orion it uses this functionality for two of its Entities, MainLog and LogEntry. Copyright © 2005 IronFlare AB |