jpa Basic mapping Entity with sequence managed Id


Example

Here we have a class and we want the identity field (userUid) to have its value generated via a SEQUENCE in the database. This SEQUENCE is assumed to be called USER_UID_SEQ, and can be created by a DBA, or can be created by the JPA provider.

@Entity
@Table(name="USER")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="USER_UID_GENERATOR", sequenceName="USER_UID_SEQ")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="USER_UID_GENERATOR")
    private Long userUid;

    @Basic
    private String userName;
}