Download PDF Home History |  | API Search Feedback FAQ

> ## Primary Keys for Bean-Managed Persistence

> You specify the primary key class in the entity bean's deployment descriptor. In most cases, your primary key class will be a `String`, an `Integer`, or some other class that belongs to the J2SE or J2EE standard libraries. For some entity beans, you will need to define your own primary key class. For example, if the bean has a composite primary key (that is, one composed of multiple fields), then you must create a primary key class.

> ### The Primary Key Class

> The following primary key class is a composite key, the `productId` and `vendorId` fields together uniquely identify an entity bean.

>     public class ItemKey implements java.io.Serializable {

>        public String productId;
>        public String vendorId;

>        public ItemKey() { };

>        public ItemKey(String productId, String vendorId) {

>          this.productId = productId;
>          this.vendorId = vendorId;
>        }

>        public String getProductId() {

>           return productId;
>        }

>        public String getVendorId() {

>           return vendorId;
>        }

>        public boolean equals(Object other) {

>           if (other instanceof ItemKey) {
>              return (productId.equals(((ItemKey)other).productId)
>                      && vendorId.equals(((ItemKey)other).vendorId));
>           }
>           return false;
>        }

>        public int hashCode() {

>           return productId.concat(vendorId).hashCode();
>        }
>     }

> For bean-managed persistence, a primary key class must meet these requirements:

> - The access control modifier of the class must be `public`.
> - All fields must be declared as `public`.
> - The class must have a public default constructor.
> - The class must implement the `hashCode()` and `equals(Object other)` methods.
> - The class must be serializable.

> ### Primary Keys in the Entity Bean Class

> With bean-managed persistence, the `ejbCreate` method assigns the input parameters to instance variables and then returns the primary key class:

>     public ItemKey ejbCreate(String productId, String vendorId,
>        String description) throws CreateException {

>        if (productId == null || vendorId == null) {
>           throw new CreateException(
>                     "The productId and vendorId are required.");
>        }

>        this.productId = productId;
>        this.vendorId = vendorId;
>        this.description = description;

>        return new ItemKey(productId, vendorId);
>     }

> The `ejbFindByPrimaryKey` verifies the existence of the database row for the given primary key:

>     public ItemKey ejbFindByPrimaryKey(ItemKey primaryKey)
>        throws FinderException {

>        try {
>           if (selectByPrimaryKey(primaryKey))
>              return primaryKey;
>        ...
>     }

>     private boolean selectByPrimaryKey(ItemKey primaryKey)
>        throws SQLException {

>        String selectStatement =
>              "select productid " +
>              "from item where productid = ? and vendorid = ?";
>        PreparedStatement prepStmt =
>              con.prepareStatement(selectStatement);
>        prepStmt.setString(1, primaryKey.getProductId());
>        prepStmt.setString(2, primaryKey.getVendorId());
>        ResultSet rs = prepStmt.executeQuery();
>        boolean result = rs.next();
>        prepStmt.close();
>        return result;
>     }

> ### Getting the Primary Key

> A client can fetch the primary key of an entity bean by invoking the `getPrimaryKey` method of the `EJBObject` class:

>     SavingsAccount account;
>     ...
>     String id = (String)account.getPrimaryKey();

> The entity bean retrieves its own primary key by calling the `getPrimaryKey` method of the `EntityContext` class:

>     EntityContext context;
>     ...
>     String id = (String) context.getPrimaryKey();

Download PDF Home History |  | API Search Feedback FAQ

All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.