JPA简单实例

1、JPA

JPA(Java持久性API)是存储业务实体关联的实体的来源。它显示了如何定义一个面向普通Java对象(POJO)作为一个实体,以及如何管理关系实体。

2、安装JPA

(1)创建一个新的JPA项目,首先选择File->New->JPA Project。

(2)输入项目名称JPA01,选择Target runtime jre7如下。

JPA简单实例_第1张图片

(3)点击下载库(如果没有库)中的用户库部分,在下载库对话框中选择EclipseLink库的最新版本,点击下一步。然后接受许可条款,单击Finish。下载后,选择用户库部分中的下载库,然后单击Finish完成操作。

JPA简单实例_第2张图片

JPA简单实例_第3张图片

JPA简单实例_第4张图片

JPA简单实例_第5张图片

JPA简单实例_第6张图片

JPA简单实例_第7张图片

3、简单实例

(1)目录结构

JPA简单实例_第8张图片

(2)persistence.xml



	
		cn.hwd.jpa.entity.Employee
		
        	
           	
         	
          	
           	
          	
       
	

(3)Employee.java

package cn.hwd.jpa.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
public class Employee {

	@Id
	@GeneratedValue(strategy= GenerationType.AUTO)
	private int id;
	private String name;
	private double salary;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Employee(int id, String name, double salary) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", salary=" + salary
				+ "]";
	}
}

(4)EmployeeServiceTest.java

package cn.hwd.jpa.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.Test;

import cn.hwd.jpa.entity.Employee;

public class EmployeeServiceTest {

	@Test
	public void createEmployee() {
		EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("JPA01");
	   	EntityManager entitymanager = emfactory.createEntityManager();
	   	entitymanager.getTransaction().begin();
	   	
	   	Employee employee = new Employee(); 
	   	employee.setId(1);
	   	employee.setName("Jerry");
	   	employee.setSalary(6999);
	   	entitymanager.persist(employee);
	   	entitymanager.getTransaction().commit();
	   	
	   	entitymanager.close();
	   	emfactory.close();
	}
	
	@Test
	public void deleteEmployee() {
		EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("JPA01");
	   	EntityManager entitymanager = emfactory.createEntityManager();
	   	entitymanager.getTransaction().begin();
	   	
	   	Employee employee=entitymanager.find(Employee.class, 1);
	   	entitymanager.remove(employee);
	   	entitymanager.getTransaction().commit();
	   	
	   	entitymanager.close();
	   	emfactory.close();
	}
	
	@Test
	public void findEmployee() {
		EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("JPA01");
	   	EntityManager entitymanager = emfactory.createEntityManager();
	   	
	   	Employee employee=entitymanager.find(Employee.class, 1);
	   	System.out.println(employee);
	}
	
	@Test
	public void updateEmployee() {
		EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("JPA01");
	   	EntityManager entitymanager = emfactory.createEntityManager();
	   	entitymanager.getTransaction().begin();
	   	
	   	Employee employee=entitymanager.find(Employee.class, 1);
	   	//before update
	   	System.out.println(employee);
	   	employee.setSalary(7200);
	   	entitymanager.getTransaction().commit();
	    //after update
	   	System.out.println(employee);
	   	
	   	entitymanager.close();
	   	emfactory.close();
	}

}

你可能感兴趣的:(JPA)