JPA作为EJB 3.0标准一部分引入,使用Annotation, persistence.xml来描述数据库字段映射,主键,外键等。JPA作为一个标准(Specification),有许许多多的实现。本文使用Apache的openJPA。下面给出例子:
meta-inf/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0">
<persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>entity.Customer</class>
<class>entity.Address</class>
<class>entity.CustomerJoined</class>
<class>entity.OnlineCustomer</class>
<class>entity.CustomerSingle</class>
<class>entity.OnlineCustomerJoined</class>
<class>entity.CustomerTable</class>
<class>entity.OnlineCustomerTable</class>
<properties>
<property name="openjpa.ConnectionURL"
value="jdbc:oracle:thin:@//192.168.1.118:1521/orcl"/>
<property name="openjpa.ConnectionDriverName"
value="oracle.jdbc.OracleDriver"/>
<property name="openjpa.ConnectionUserName" value="hr"/>
<property name="openjpa.ConnectionPassword" value="a123456"/>
<property name="openjpa.Log" value="DefaultLevel=TRACE"/>
</properties>
</persistence-unit>
</persistence>
Entity: @EntityListeners({CustListner.class})
@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;
@Embedded
private Address address = new Address();
@Column(name = "CUST_TYPE", length = 10)
private String custType;
@Version
@Column(name = "LAST_UPDATED_TIME")
private Date updatedTime;
//省略getter和setter
EntityListener:指明了回调函数的类。回调函数有:prePersist(), postPersist()等。请看CustListner:
package entity;
import javax.persistence.*;
/*
* This is the callback class for CUSTOMER entity
*/
public class CustListner {
@PostLoad
public void postLoad(Customer cust) {
System.out.println("In post load");
}
@PrePersist
public void prePersist(Customer cust) {
System.out.println("In pre persist");
}
@PostPersist
public void postPersist(Customer cust) {
System.out.println("In post persist");
}
@PreUpdate
public void preUpdate(Customer cust) {
System.out.println("In pre update");
}
@PostUpdate
public void postUpdate(Customer cust) {
System.out.println("In post update");
}
@PreRemove
public void preRemove(Customer cust) {
System.out.println("In pre remove");
}
@PostRemove
public void postRemove(Customer cust) {
System.out.println("In post remove");
}
}
@Entity(name = "CUSTOMER") 指明了数据库表名。@GeneratedValue(strategy = GenerationType.AUTO) 使用JPA产生值
@Embedded: 使用另一个对象,其思想是范式化,去冗余。Address类如下,其使用@Embeddable
@Embeddable
public class Address implements Serializable{
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;
// getters and setters
public String getAppt() {
return appt;
}
public void setAppt(String appt) {
this.appt = appt;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
}