JPA入门

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;
	}
	
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值