Version 4

    @GeneratedValue Syntax

    in JBoss EJB3.0 RC4-PFD

     

    From the spec:

     

    +The GeneratedValue annotation provides for the specification of generation strategies for the values of primary keys. The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation.+

     

    Syntax:

     

    @Id
    @GeneratedValue( strategy=GenerationType.[TABLE|SEQUENCE|IDENTITY|AUTO], generator="GEN_OR_SEQ_NAME" )
    

    Both parameters are optional. The default strategy is AUTO. The behavior for these different options is not exactly defined in the spec, but "the SEQUENCE and IDENTITY values specify the use of a database sequence or identity column, respectively." The default id generator is supplied by the persistence provider (Hibernate).

     

    Example:

     

    @Id
    @GeneratedValue( strategy=GenerationType.SEQUENCE, generator="CUST_ID_SEQ" )
    @Column (name="CUST_ID")
    
    public Long getId() {return id; }
    

     

    When you use a sequence generator you also have to specify it:

     

    @Id
    @GeneratedValue( strategy=GenerationType.SEQUENCE, generator="customer_id_sequence" ) // name of the generator
    @SequenceGenerator( name="customer_id_sequence", sequenceName="CUST_ID_SEQ" ) // name of the sequence in the db
    @Column (name="CUST_ID") // name of the column in the db
    public Long getId() { return id; }