Showing posts with label orm. Show all posts
Showing posts with label orm. Show all posts

Tuesday, October 27, 2009

Hibernate Sequence Generator and H2

Below you can find an example of how to create an ORM with a generated id. The id here is generated via H2 database:

The class file:
---------------

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "some_table")
@javax.persistence.SequenceGenerator(name = "SEQ_SOME_TABLE", sequenceName = "SEQ_SOME_TABLE")
public class SomeOrm {

protected Long id;
protected String text;

public SomeOrm() {

}

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

public void setId(Long id) {
this.id = id;
}

@Column(name = "text")
public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

}

The code for the database table:
-------------------------------------

CREATE SEQUENCE SEQ_SOME_TABLE;

create table some_table (
some_id BIGINT DEFAULT (NEXT VALUE FOR SEQ_SOME_TABLE) not null primary key,
text VARCHAR2
);