[jboss-user] [EJB 3.0] - Re: Sequence Generator Question

oskar.carlstedt do-not-reply at jboss.com
Tue Jan 29 02:34:18 EST 2008


Hi Kasim!

Sequenzes are not hibernate specific "stuff". Instead sequences are database specific counters. Oracle are using sequnces, but MsSQL, MySQL, etc. are using identity mappings. If you are using MySQL you probably use an auto_increment to get the same feature as an Oracle sequence.

Setting the strategy attribute (of the GeneratedValue-annotation) to AUTO will let Hibernate choose the strategy depending on the underlying database. I.e. Oracle will use sequence, MySQL will use identity.

This is what you shall do to be sure you write an implementation that works for many database providers/brands.


  | 
  | ...
  | 
  | @Entity
  | @Table(name="the_object")
  | public class TheObject {
  | 
  |    ...
  | 
  |    @Id
  |    @Column(name="id")
  |    @GeneratedValue(strategy=GenerationType.AUTO,  generator="seqTheObject")
  |    @SequenceGenerator(name="seqTheObject", sequenceName="SEQ_THE_OBJECT")
  |    private Long id;
  | 
  |    ...
  | 
  | }
  | 

In this case, if you choose to use Oracle that is using sequnces, then you must have created a sequence SEQ_THE_OBJECT declared in your Oracle database schema. You get a create table syntax like:


  | CREATE TABLE THE_OBJECT (
  |    ID NUMBER(10) NOT NULL,
  |    ...
  |    PRIMARY KEY(ID)
  | );
  | 
  | CREATE SEQUENCE SEQ_THE_OBJECT START WITH 1 INCREMENT BY 1;
  | 
  | 

On the other hand, if you choose MySQL as your database provider, then you set the id column to use "AUTO_INCREMENT".



  | CREATE TABLE THE_OBJECT (
  |    ID INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  |    ...
  |    PRIMARY KEY(ID)
  | )
  | 


I hope this answered your question.

Cheers
/Oskar



View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4124271#4124271

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4124271



More information about the jboss-user mailing list