具体例子
完成工程后工程结构如下图:
(注意:persistence.xml文件的位置决定持久性的根(Persistence Root)。持久性的根为JAR文件或者包含META-INF目录(前提是persistence.xml位于此)的目录。一般将这个persistence.xml文件放在src下的META-INF中。命名及位置都不能变)
a)、导入相关jar包(见上图)和创建META-INF和persistence.xml文件.
persistence.xml配置如下:(我用的数据为MySQL,采用不同数据库及JPA的不同实现版本会导致配置内容不同)
[xhtml] view plain copy
- <persistence xmlns="http://java.sun.com/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_version=1">
- <persistence-unit name="mysqlJPA" transaction-type="RESOURCE_LOCAL">
- <properties>
- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
- <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
- <property name="hibernate.connection.username" value="root" />
- <property name="hibernate.connection.password" value="123456" />
- <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/db1" />
- <property name="hibernate.max_fetch_depth" value="3" />
- <property name="hibernate.hbm2ddl.auto" value="update" />
- </properties>
- </persistence-unit>
- </persistence>
b)、编写实体bean,如下:
[c-sharp] view plain copy
- package com.hmk.bean;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- @Entity
- public class Person {
- private int id;
- private String name;
- @Id @GeneratedValue
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- @Column(length=12)
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
c)、编写junit测试代码,如下:
[java] view plain copy
- package junit.test;
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import com.hmk.bean.Person;
- public class JpaTest {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- }
- @Test public void createTable(){
- //可以验证生成表是否正确
- EntityManagerFactory factory = Persistence.createEntityManagerFactory("mysqlJPA");
- factory.close();
- }
- @Test public void save(){
- EntityManagerFactory factory = Persistence.createEntityManagerFactory("mysqlJPA");
- EntityManager em = factory.createEntityManager();
- em.getTransaction().begin();
- Person person = new Person(); //person为new状态
- person.setName("zhang san");
- em.persist(person); //持久化实体
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- //new 、托管、脱管、删除
- @Test public void update(){
- EntityManagerFactory factory = Persistence.createEntityManagerFactory("mysqlJPA");
- EntityManager em = factory.createEntityManager();
- em.getTransaction().begin();
- Person person = em.find(Person.class, 1);
- person.setName("hmk"); //person为托管状态
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- @Test public void update2(){
- EntityManagerFactory factory = Persistence.createEntityManagerFactory("mysqlJPA");
- EntityManager em = factory.createEntityManager();
- em.getTransaction().begin();
- Person person = em.find(Person.class, 1);
- em.clear(); //把实体管理器中的所有实体变为脱管状态
- person.setName("hmk2");
- em.merge(person); //把脱管状态变为托管状态,merge可以自动选择insert or update 数据
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- @Test public void remove(){
- EntityManagerFactory factory = Persistence.createEntityManagerFactory("mysqlJPA");
- EntityManager em = factory.createEntityManager();
- em.getTransaction().begin();
- Person person = em.find(Person.class, 1);
- em.remove(person); //删除实体
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- @Test public void find(){
- EntityManagerFactory factory = Persistence.createEntityManagerFactory("mysqlJPA");
- EntityManager em = factory.createEntityManager();
- Person person = em.find(Person.class, 2); //类似于hibernate的get方法,没找到数据时,返回null
- System.out.println(person.getName());
- em.close();
- factory.close();
- }
- @Test public void find2(){
- EntityManagerFactory factory = Persistence.createEntityManagerFactory("mysqlJPA");
- EntityManager em = factory.createEntityManager();
- Person person = em.getReference(Person.class, 2); //类似于hibernate的load方法,延迟加载.没相应数据时会出现异常
- System.out.println(person.getName()); //真正调用时才查找数据
- em.close();
- factory.close();
- }
- }
d)、运行junit测试代码里的相应方法就行可以。
时间: 2024-10-26 17:17:24