|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 6
You'll need three artifacts to implement a JPA-compliant program:
JPA is all about data persistence, so let's begin our examination of how it works with the data store design. Assume you have a CUSTOMER table, as in Table 1 below.
| NAME | PK? | TYPE | NULL? |
|---|---|---|---|
| CUST_ID | Y | INTEGER | NOT NULL |
| FIRST_NAME | VARCHAR(50) | NOT NULL | |
| LAST_NAME | VARCHAR(50) | ||
| STREET | VARCHAR(50) | ||
| APPT | VARCHAR(20) | NOT NULL | |
| CITY | VARCHAR(25) | ||
| ZIP_CODE | VARCHAR(10) | NOT NULL | |
| CUST_TYPE | VARCHAR(10) | NOT NULL | |
| LAST_UPDATED_TIME | TIMESTAMP | NOT NULL |
Since JPA is all about entity-relationship mapping, next you need to take a look at the design of the Customer entity object. The entity object is nothing but a POJO class marked as an entity with the @Entity annotation, as you can see in Listing 1.
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@Entity(name = "CUSTOMER") //Name of the entity
public class Customer implements Serializable{
private long custId;
private String firstName;
private String lastName;
private String street;
private String appt;
private String city;
private String zipCode;
private String custType;
private Date updatedTime;
// Getters and setters go here
......................
}
The Customer entity needs to know how to map the attributes (or properties) to the CUSTOMER table. You can do this either through a configuration
file called orm.xml (similar to a .hbm file in Hibernate) or, as shown in Listing 2, through JPA annotations.
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@Entity(name = "CUSTOMER") //Name of the entity
public class Customer implements Serializable{
@Id //signifies the primary key
@Column(name = "CUST_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long custId;
@Column(name = "FIRST_NAME", nullable = false,length = 50)
private String firstName;
@Column(name = "LAST_NAME", length = 50)
private String lastName;
// By default column name is same as attribute name
private String street;
@Column(name = "APPT",nullable = false)
private String appt;
// By default column name is same as attribute name
private String city;
@Column(name = "ZIP_CODE",nullable = false)
// Name of the corresponding database column
private String zipCode;
@Column(name = "CUST_TYPE", length = 10)
private String custType;
@Version
@Column(name = "LAST_UPDATED_TIME")
private Date updatedTime;
// Getters and setters go here
......................
}
Take a closer look at the annotations defined in Listing 2.
javax.persistence, so you'll need to import that package.
@Enitity signifies that a particular class is an entity class. If the entity name is different from the table name, then the @Table annotation is used; otherwise, it isn't required.
@Column provides the name of the column in a table if it is different from the attribute name. (By default, the two names are assumed
to be the same.)
@Id signifies the primary key.
@Version signifies a version field in an entity. JPA uses a version field to detect concurrent modifications to a data store record.
When the JPA runtime detects multiple attempts to concurrently modify the same record, it throws an exception to the transaction
attempting to commit last. This prevents you from overwriting the previous commit with stale data.
@Basic, which are persisted as-is in the database.
@GeneratedValue signifies a strategy to assign a unique value to your identity fields automatically. The types of strategies available are IDENTITY, SEQUENCE,
TABLE, and AUTO. The default strategy is auto, the implementation of which is left to the JPA vendor to implement. (OpenJPA
implements it through a table sequence.)
There are a few points to keep in mind when creating an entity class:
java.net.Socket and java.lang.Thread.
Archived Discussions (Read only)