Chapter 8 Creating J2EE Applications for BEA WebLogic


Primary Keys

The primary key is an object that uniquely identifies an entity bean within its home. The container must be able to manipulate the primary key of an entity bean. The primary key is specified in the deployment descriptor. You can specify a primary key class for an entity bean with container-managed persistence by mapping the primary key to either a single field or to multiple fields in the entity bean class.

Primary Key Mapped to a Single CMP Field

In the entity bean class, you can have a primary key that maps to a single CMP field. You use the primkey-field element, a deployment descriptor in the ejb-jar.xml file to specify the container-managed field that is the primary key. The prim-key-class element must be the primary key field's class.

Primary Key Class that Wraps Single or Multiple CMP Fields

You can have a primary key class that maps to single or multiple fields. The primary key class must be public, and have a public constructor with no parameters. You use the prim-key-class element, a deployment descriptor in the ejb-jar.xml file to specify the name of the entity bean's primary key class. You can only specify the class name in this deployment descriptor element. All of the fields in the primary key class must be declared as public. The fields in the class must have the same name as the primary key fields in the ejb-jar.xml file.

Additional Help for Primary Keys

Some hints for using primary keys with WebLogic Server include:

A cmp field of the type BigDecimal should not be used as a primary key field for CMP beans. The boolean BigDecimal.equals (object x) method considers two BigDecimal equal only if they are equal in value and scale. This is because there are differences in precision between the Java language and different databases. For example, the method does not consider 7.1 and 7.10 to be equal. Consequently, this method will most likely return false or cause the CMP bean to fail.

If you need to use BigDecimal as the primary key, you should:

  1. Implement a primary key class.
  2. In this primary key class, implement the boolean equal (Object x) method.
  3. In the equal method, use boolean BigDecimal.compareTo(BigDecimal val).

Mapping to a database column

WebLogic Server supports mapping a database column to a cmp-field and a cmr-field concurrently. The cmp-field is read-only in this case. If the cmp-field is a primary key field, the value for the field must be set during create, using the setXXX method for the cmp-field.

 


Copyright (C) 2005. Sybase Inc. All rights reserved.