Understanding JPA, 2

来源:互联网 发布:java如何获取当前日期 编辑:程序博客网 时间:2024/05/21 00:52
原文:http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html?page=2

Page 2 of 6

JPA: How to?

You'll need three artifacts to implement a JPA-compliant program:

  • An entity class
  • A persistence.xml file
  • A class through which you will insert, update, or find an entity

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.

Table 1. CUSTOMER table schema

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

The persistence object: Entity

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.

Listing 1. The Customer entity

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 Customerentity needs to know how to map the attributes (or properties) to theCUSTOMER table. You can do this either through a configuration filecalled orm.xml (similar to a .hbm file in Hibernate) or, as shown inListing 2, through JPA annotations.

Listing 2. The Customer entity with 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.

  • The annotations are defined in 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.
  • @Versionsignifies a version field in an entity. JPA uses a version field todetect concurrent modifications to a data store record. When the JPAruntime detects multiple attempts to concurrently modify the samerecord, it throws an exception to the transaction attempting to commitlast. This prevents you from overwriting the previous commit with staledata.
  • By default, all the fields are of type @Basic, which are persisted as-is in the database.
  • @GeneratedValue signifies a strategyto assign a unique value to your identity fields automatically. Thetypes of strategies available are IDENTITY, SEQUENCE, TABLE, and AUTO.The default strategy is auto, the implementation of which is left tothe JPA vendor to implement. (OpenJPA implements it through a tablesequence.)

There are a few points to keep in mind when creating an entity class:

  • JPA allows persistent classes to inherit from non-persistent classes, persistent classes to inherit from other persistent classes, and non-persistent classes to inherit from persistent classes.
  • The entity class should have a default no-argument constructor.
  • The entity class should not be final.
  • Persistent classes cannot inherit from certain natively-implemented system classes such as java.net.Socket and java.lang.Thread.
  • If a persistent class inherits from a non-persistent class, the fields of the non-persistent super class cannot be persisted.

原创粉丝点击