|
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 3 (10.1.3) B14428-01 |
|
![]() Previous |
![]() Next |
Every EJB 3.0 entity must have a primary key field (see "Configuring an EJB 3.0 Entity Primary Key Field").
You can specify a primary key as a single primitive or JDK object type.
You can either assign primary key values yourself, or, more typically, you can associate a primary key field with a primary key value generator (see "Configuring EJB 3.0 Entity Automatic Primary Key Generation").
You must specify one entity field as the primary key. You can specify a primary key as a single primitive or JDK object type or as a composite primary key class made up of one or more primitive or JDK object types
Typically, you associate the primary key field with a primary key value generator (see "Configuring EJB 3.0 Entity Automatic Primary Key Generation").
|
Note: For an EJB 3.0 basic mapping code example, see:http://www.oracle.com/technology/tech/java/oc4j/ejb3/howtos-ejb3/howtoejb30mappingannotations/doc/how-to-ejb30-mapping-annotations.html#id
|
Example 7-1 shows how to use the @Id annotation to specify an entity field as the primary key. In this example, primary key values are generated using a table generator (see "Configuring EJB 3.0 Entity Automatic Primary Key Generation").
Typically, you associate a primary key field (see "Configuring an EJB 3.0 Entity Primary Key Field") with a primary key value generator so that when an entity instance is created, a new, unique primary key value is assigned automatically.
Table 7-2 lists the types of primary key value generators that you can define.
Table 7-2 EJB 3.0 Entity Primary Key Value Generators
| Type | Description | For more information, see ... |
|---|---|---|
|
Generated Id Table |
A database table the container uses to store generated primary key values for entities. Typically shared by multiple entity types that use table-based primary key generation. Each entity type will typically use its own row in the table to generate the primary key values for that entity class. Primary key values are positive integers. |
"Table Sequencing" in the Oracle TopLink Developer's Guide |
|
Table Generator |
A primary key generator which you can reference by name, defined at one of the package, class, method, or field level. The level at which you define it will depend upon the desired visibility and sharing of the generator. No scoping or visibility rules are actually enforced. Oracle recommends that you define the generator at the level for which it will be used. This generator is based on a database table. |
"Table Sequencing" in the Oracle TopLink Developer's Guide |
|
Sequence Generator |
A primary key generator which you can reference by name, defined at one of the package, class, method, or field level. The level at which you define it will depend upon the desired visibility and sharing of the generator. No scoping or visibility rules are actually enforced. Oracle recommends that you define the generator at the level for which it will be used. This generator is based on a sequence object that the database server provides. |
"Native Sequencing With an Oracle Database Platform" in the Oracle TopLink Developer's Guide "Native Sequencing With a Non-Oracle Database Platform" in the Oracle TopLink Developer's Guide |
|
Note: For an EJB 3.0 automatic primary key generation code example, see:http://www.oracle.com/technology/tech/java/oc4j/ejb3/howtos-ejb3/howtoejb30mappingannotations/doc/how-to-ejb30-mapping-annotations.html#sequencing
|
Example 7-2 shows how to use the @GeneratedIdTable annotation to specify a primary key value generator based on a database table. When a new instance of Employee is created, a new value for entity field id is obtained from EMPLOYEE_GENERATOR_TABLE.
Example 7-2 @GeneratedIdTable
@Entity
@Table(name="EJB_EMPLOYEE")
@GeneratedIdTable(
name="EMPLOYEE_GENERATOR_TABLE",
table=@Table(name="EJB_EMPLOYEE_SEQ"),
pkColumnName="SEQ_NAME", valueColumnName="SEQ_COUNT"
)
public class Employee implements Serializable
{
...
@Id(generate=TABLE, generator="EMPLOYEE_GENERATOR_TABLE")
@Column(name="EMPLOYEE_ID", primaryKey=true)
public Integer getId()
{
return id;
}
...
}
Example 7-3 shows how to use the @TableGenerator annotation to specify a primary key value generator based on a database table. When a new instance of Employee is created, a new value for entity field id is obtained from EMPLOYEE_GENERATOR_TABLE. You must set the @Id annotation attribute generate to TABLE in this case.
Example 7-3 @TableGenerator
@Entity
@Table(name="EJB_ADDRESS")
public class Address implements Serializable
{
...
@Id(generate=TABLE, generator="ADDRESS_TABLE_GENERATOR")
@TableGenerator(
name="ADDRESS_TABLE_GENERATOR",
tableName="EMPLOYEE_GENERATOR_TABLE",
pkColumnValue="ADDRESS_SEQ"
)
@Column(name="ADDRESS_ID")
public Integer getId()
{
return id;
}
...
}
Example 7-3 shows how to use the @SequenceGenerator annotation to specify a primary key value generator based on a sequence object provided by the database. When a new instance of Employee is created, a new value for entity field id is obtained from database sequence object ADDRESS_SEQ. You must set the @Id annotation attribute generate to SEQUENCE in this case.
Example 7-4 @SequenceGenerator
@Entity
@Table(name="EJB_ADDRESS")
public class Address implements Serializable
{
...
@Id(generate=SEQUENCE, generator="ADDRESS_TABLE_GENERATOR")
@SequenceGenerator(
name="ADDRESS_TABLE_GENERATOR",
sequenceName="ADDRESS_SEQ"
)
@Column(name="ADDRESS_ID")
public Integer getId()
{
return id;
}
...
}