Id generation in Hibernate with Sequence

Posted by {"name"=>"Palash Ray", "email"=>"paawak@gmail.com", "url"=>"https://www.linkedin.com/in/palash-ray/"} on October 31, 2010 · 1 min read

I have the following SQL Script:

CREATE SEQUENCE  MY_SEQ
START WITH 1
INCREMENT BY 1;
CREATE TABLE  SEQ_DEMO (
	ID             INTEGER PRIMARY KEY NOT NULL,
	NAME     VARCHAR2(20) NOT NULL
    ) ;

The Hibernate entity for this would be:

@Entity
@Table(name = "SEQ_DEMO")
public class SeqDemo {
    @Id
    @SequenceGenerator(name = "seq", allocationSize = 1, initialValue = 1, sequenceName = "MY_SEQ")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
    private long id;
    @Column
    private String name;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}