JPA入门

来源:互联网 发布:淘宝集运到菲律宾 编辑:程序博客网 时间:2024/05/16 02:03

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 entitypublic 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;@Embeddedprivate 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 {@PostLoadpublic void postLoad(Customer cust) {        System.out.println("In post load");    }@PrePersistpublic void prePersist(Customer cust) {        System.out.println("In pre persist");    }@PostPersistpublic void postPersist(Customer cust) {        System.out.println("In post persist");    }@PreUpdatepublic void preUpdate(Customer cust) {        System.out.println("In pre update");            }@PostUpdatepublic void postUpdate(Customer cust) {        System.out.println("In post update");    }@PreRemovepublic void preRemove(Customer cust) {        System.out.println("In pre remove");    }@PostRemovepublic void postRemove(Customer cust) {        System.out.println("In post remove");    }}
@Entity(name = "CUSTOMER") 指明了数据库表名。

@GeneratedValue(strategy = GenerationType.AUTO) 使用JPA产生值

@Embedded: 使用另一个对象,其思想是范式化,去冗余。Address类如下,其使用@Embeddable

@Embeddablepublic class Address implements Serializable{private String street;@Column(name = "APPT",nullable = false)  private String appt;// By default column name is same as attribute nameprivate String city;@Column(name = "ZIP_CODE",nullable = false)  // Name of the corresponding database columnprivate String zipCode;// getters and setterspublic 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;}}



0 0
原创粉丝点击