Wednesday, November 21, 2012

Hibernate Custom Sequence For Oracle

Create sequence annotation for Hibernate entity using Oracle database starting at a custom number.
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,
  generator="entity_seq_gen" )
@GenericGenerator(name="entity_seq_gen",
  strategy = "sequence",
  parameters = { @Parameter(name="sequence", value="ENTITY_SEQ"),
                 @Parameter(name ="parameters",
                            value="START WITH 844170")})
@Column(name = "entity_id")
private Long entityId;

Thus, generating this SQL statement
CREATE SEQUENCE  "ENTITY_SEQ"
       MINVALUE 1
       MAXVALUE 9999999999999999999999999999
       INCREMENT BY 1
       START WITH 844190
       CACHE 20
       NOORDER
       NOCYCLE ;
Remove this line if you want to use the default starting number of your sequence:

@Parameter(name ="parameters", value="START WITH 844170" )



No comments:

Post a Comment