Create a primary key column with sequence generated value in hibernate

For example you have a class User:

package org.kalidad.seamexercises.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "USER")
@SequenceGenerator(sequenceName = "USER_SEQ_ID_GENERATOR", name = "USER_SEQ_ID_GENERATOR")
public class User implements Serializable {
	private static final long serialVersionUID = 4707868996148508582L;
	private long id;
	private String name;
	private String username;
	private String password;

	public User() {

	}

	@Column(name = "ID")
	@javax.persistence.Id
	@GeneratedValue(generator = "USER_SEQ_ID_GENERATOR")
	public long getId() {
		return id;
	}

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

	@Column(name = "NAME")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "USERNAME")
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "PASSWORD")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
Create a primary key column with sequence generated value in hibernate Create a primary key column with sequence generated value in hibernate Reviewed by Edward Legaspi on Tuesday, August 09, 2011 Rating: 5

No comments:

Powered by Blogger.