Tuesday, October 20, 2009

Create a table with autonumber/Sequence - Step By Step

Step1 : Create a sequence.
create sequence seq_name start with 1 increment by 1;

Step2: create a trigger.

create or replace trigger trigger_name before insert on table name referencing new as new for each row
begin
select seq_name.nextval into :new.id from dual;
end;
/
Note: id - field name that need to generate auto number.

Step 3. commit

Step 4. create entity bean from table

Modify the entity bean with below code.

@SequenceGenerator(name = "generator_name", sequenceName = "seq_name",
allocationSize = 1)

public class TableName implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator = "attendancegenerator")
@Column(nullable = false)
private Long fieldname;

No comments:

Post a Comment