Many databases like Oracle support database sequence. It is very convenient to use database sequence to generate primary key values. The good news is that EJB 3.0 standardizes the use of database sequence with entity beans as primary key. Also it supports sequence table approach to generate Primary Key values for entity beans.
Oracle Application Server EJB 3.0 Preview supports these type of sequencing and in this blog entry I will describe how to use these approaches for sequencing with EJB 3.0 entity beans.
For using any generated value for primary key, you have to specify the generator type by annotating with @Id. For example,
@Id(generate=TABLE, generator="ADDRESS_TABLE_GENERATOR")
Here is the annotation specification for Id:
@Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface Id { GeneratorType generate() default NONE; String generator() default ""; }
GeneratorType specified by Id.generate() imposes the following restrictions on Id.generator():
· NONE - no generator should be specified, no generator would be used.
· AUTO - no generator should be specified, container will use generator named "SEQ_GEN", which uses container defaults (unless specified by user).
· ENTITY, SEQUENCE - generator should be a SequenceGenerator and can be used with a native sequence
· TABLE - generator should be a TableGenerator can be used sequence table approach
Using Native Database Sequence
If you want to use a database sequence for generating PK values for your entities you have to use SEQUENCE as the GeneratorType.
In this example, I’ll use a simple Employee entity bean mapped to the EMP table and use a sequence named EMP_SEQ.
1. First create the sequence as follows:
create sequence emp_seq start with 1 increment by 1;
2. Define the annotation to use the EMP_SEQ in your entity bean as follows:
@Id(generate=SEQUENCE,generator="EMP_SEQ_GEN")
@SequenceGenerator(name="EMP_SEQ_GEN", sequenceName="EMP_SEQ",initialValue=1,allocationSize=1)
@Column(name="EMPNO", primaryKey=true)
public int getEmpNo()
{
return empNo;
}
Using a sequence table
However many databases do not have support for database sequence. Some developers prefer to use a Sequence Table instead of a database sequence to generate primary key values. This is also a good approach because a single table is shared for generating sequences for many entities in an application.
If you want to use this approach, first you have to create a sequence table and create an entry for the Sequence Generator for your employee e.g.
CREATE TABLE EJB_EMPLOYEE_SEQ (SEQ_COUNT NUMBER(15) NOT NULL, SEQ_NAME VARCHAR2(80) NOT NULL, PRIMARY KEY (SEQ_NAME));
INSERT INTO EJB_EMPLOYEE_SEQ(SEQ_NAME, SEQ_COUNT) VALUES ('EMPLOYEE_SEQ', 10);
commit;
You have to annotate your entity with @GeneratedIdTable to specify the table (we created earlier) that may be used by the container to store generated id values for entities as follows:
@GeneratedIdTable(name="EMPLOYEE_GENERATOR_TABLE", table=@Table(name="EJB_EMPLOYEE_SEQ"), pkColumnName="SEQ_NAME", valueColumnName="SEQ_COUNT")
Make sure that you are using the correct Table and column names for the Id Generator.
Then you have to use this generator in conjunction with @Id annotation as follows:
@Id(generate=TABLE, generator="EMPLOYEE_TABLE_GENERATOR")
@TableGenerator(name="EMPLOYEE_TABLE_GENERATOR", tableName="EMPLOYEE_GENERATOR_TABLE", pkColumnValue="EMPLOYEE_SEQ")
@Column(name="EMP_ID", primaryKey=true)
public Integer getId() {
return id;
}
For details description of all metadata annotations available for O-R mapping with EJB 3.0, look at the how-to-mapping-annotation.
11:00:22 AM
|