Entering content frame

Procedure documentation Creating Primary Keys Locate the document in its SAP Library structure

Use

You can specify the primary key for your container-managed entity bean when you create the bean, or later when you develop the bean business logic.

The bean’s primary key can be a:

·        Simple primary key – consists of one persistent field

·        Compound primary key – consists of several persistent fields

·        Unknown primary key – the primary key is unknown for the bean provider; at runtime the EJB Container generates automatically the values for that primary key.

The following information explains how you specify that the EJB Container generates the primary keys and how you create a simple primary key, and a compound primary key.

Prerequisites

The enterprise bean must have been created.

Procedure

By default, the NetWeaver Developer Studio assumes that the primary key is unknown, that is, the primary key will be generated automatically by the EJB Container.

Creating a Simple Primary Key

       1.      Create the persistent field.

       2.      In the Fields tab, select PrimaryKey.

       3.      In the right pane, choose the Simple option.

       4.      From the Persistent fields drop-down list, choose the persistent field that you want to specify as a primary key for the entity bean.

Creating a Compound Primary Key

...

       1.      Create the persistent fields.

       2.      In the Fields tab, select PrimaryKey.

       3.      In the right pane, choose the Compound option.

       4.      In the Fully Qualified Name field, specify the name of the primary key class, which your bean will use:

¡        If you have already created your primary key class, choose Choose to browse and select it.

¡        If you want to create your primary key class at this step, choose New and specify the name of the primary key class.

The NetWeaver Developer Studio will generate the primary key class.

Recommendation

We recommend that you specify the package of the primary key class.

       5.      From the right-hand pane, choose the persistent fields that you want to be part of the bean’s compound primary key.

Automatic Primary Key Generation

If your container-managed entity beans do not have a natural primary key, you can omit specifying the primary key class or the primary key fields and rely on the EJB Container to generate your primary keys. In this case, the system assumes that the primary key is unknown.

Follow the steps below to set the EJB Container to generate the bean’s primary key automatically.

...

       1.      On the Fields tab, select Primary Key.

       2.      In the right pane, choose the Unknown option.

The NetWeaver Developer Studio then:

                            a.      Creates the default ejb_pk field in persistent.xml.

                            b.      Sets java.lang.Object as the type of the primary key class. That is, the value of the <prim-key-class> element in ejb-jar.xml is set to java.lang.Object.

                            c.      Automatically sets the type of the argument of the bean’s findByPrimaryKey method to java.lang.Object.

The EJB Container will generate a system primary key field of type java.lang.Long.

       3.      In the persistent.xml file, specify the mapping of this field to the column in the database where the values of the primary key will be stored.

The JDBC type of this column must be java.sql.Types.BIGINT and this column must be the only primary key column in the database table. For more information, see Object/Relational Mapping Rules.

       4.      Specify the primary key deployment properties, that is specify the unknown primary key interval.

 

See also:

Mapping Persistent Fields (cmp-fields)

 

 

Leaving content frame