这个类只包含了三个属性,为了把Employee转化为一个实体,首先需要以@Entity注解该类。它仅仅是一个标记注解,用于通知持久化引擎类是一个实体。@Id它注解了特定字段用于保存实体持久化标识的主键。关于多数的注解和用法在这里不一一展开。
EmployeeService类:
package com.silence.test;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import com.silence.entities.Employee;
public class EmployeeService {
protected EntityManager em;
public EmployeeService(EntityManager em) {
this.em = em;
}
public Employee createEmployee(int id, String name, long salary) {
Employee emp = new Employee();
emp.setName(name);
emp.setSalary(salary);
em.persist(emp);
return emp;
}
public void removeEmployee(int id) {
Employee emp = findEmployee(id);
if (emp != null) {
em.remove(emp);
}
}
public Employee raiseEmployeeSalary(int id, long raise) {
Employee emp = em.find(Employee.class, id);
if (emp != null) {
emp.setSalary(emp.getSalary() + raise);
}
return emp;
}
public Employee findEmployee(int id) {
return em.find(Employee.class, id);
}
public List<Employee> findAllEmployees() {
TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e", Employee.class);
return query.getResultList();
}
}
其中em是一个实体管理器,它来自EntityManagerFactory类型的工厂,就像hibernate中的Session和SessionFactory
的关系。
package com.silence.test;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.silence.entities.Employee;
public class test {
private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;
private EmployeeService service = null;
@Before
public void getEntityManagerFactory(){
entityManagerFactory = Persistence.createEntityManagerFactory("JPA-API1");
entityManager = entityManagerFactory.createEntityManager();
service = new EmployeeService(entityManager);
entityManager.getTransaction().begin();
}
@After
public void closeManager(){
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
}
//查找所有Employee操作
@Test
public void findAll(){
List<Employee> list = service.findAllEmployees();
for(Employee e:list){
System.out.println(e);
}
}
//查找Employee操作
@Test
public void find(){
Employee emp = service.findEmployee(1);
System.out.println(emp);
}
//添加Employee操作
@Test
public void testAdd(){
Employee em = service.createEmployee(4, "silence1", 10000001);
System.out.println("persistence");
}
}
test方法
package com.silence.test;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.silence.entities.Employee;
public class test {
private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;
private EmployeeService service = null;
@Before
public void getEntityManagerFactory(){
entityManagerFactory = Persistence.createEntityManagerFactory("JPA-API1");
entityManager = entityManagerFactory.createEntityManager();
service = new EmployeeService(entityManager);
entityManager.getTransaction().begin();
}
public static void main(String[] args) {
}
@After
public void closeManager(){
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
}
@Test
public void deleteEmployee(){
Employee emp = service.removeEmployee(1);
System.out.println(emp);
}
@Test
public void upateEmployee(){
Employee emp = service.raiseEmployeeSalary(1, 120);
System.out.println(emp);
}
@Test
public void findAll(){
List<Employee> list = service.findAllEmployees();
for(Employee e:list){
System.out.println(e);
}
}
@Test
public void find(){
Employee emp = service.findEmployee(1);
System.out.println(emp);
}
@Test
public void testAdd(){
Employee em = service.createEmployee(4, "silence1", 10000001);
System.out.println("persistence");
}
}
因为程序作为一个简单的javaSE应用程序运行,需要把相应的jar文件加入到classpath下,同时,需要加入mysql数据库驱动,JPA实现茶品hibernate的相应的jar包。