Java Reference
In-Depth Information
Primary Key Generation Strategy Description
GenerationType.SEQUENCE
Indicates that a database sequence should be used to
generate the entity's primary key value.
GenerationType.TABLE
Indicates that a database table should be used to
generate the entity's primary key value.
In most cases, the GenerationType.AUTO strategy works properly, therefore it is
almost always used, for this reason the New Entity Class wizard uses this strategy.
When using the sequence or table generation strategies, we might have to
indicate the sequence or table used to generate the primary keys. These can
be specified by using the @SequenceGenerator and @TableGenerator
annotations, respectively. Consult the Java EE 6 JavaDoc at http://
download.oracle.com/javaee/6/api/ for details.
Adding persistent fields to our entity
At this point, our JPA entity contains a single field, its primary key, admittedly not
very useful. We need to add a few fields to be persisted to the database.
package com.ensode.jpaweb;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
Search WWH ::




Custom Search