Java Reference
In-Depth Information
We can do so by simply clicking on the Finish button. At this point NetBeans
generates the source for our JPA entity.
JPA allows the primary field of a JPA entity to map to any column type
( VARCHAR , NUMBER ). It is best practice to have a numeric surrogate
primary key, that is, a primary key that serves only as an identifier and
has no business meaning in the application. Selecting the default Primary
Key type of long will allow for a wide range of values to be available for
the primary keys of our entities.
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;
private Long id;
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
//Other generated methods (hashCode(), equals() and
//toString() omitted for brevity.
}
As we can see, a JPA entity is a standard Java object. There is no need to extend any
special class or implement any special interface. What differentiates a JPA entity
from other Java objects are a few JPA-specific annotations.
 
Search WWH ::




Custom Search