[Java] JPA的测试

博客首页 » Java JPA的测试

发布于 07 Jan 2015 05:22
标签 blog
这篇文章讲解JPA的例子说得还比较清楚,只是例子写得不精细,不能支持重复地运行。稍微修改了一下,并且也提高了连接性能。

http://blog.csdn.net/hmk2011/article/details/6289151

package com.jpa.example;
 
import static org.junit.Assert.*;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
 
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
 
import com.jpa.example.Person;
 
public class PersonTest {
 
    private static EntityManagerFactory factory;
    private EntityManager em;  
 
    @BeforeClass  
    public static void setUpBeforeClass() throws Exception {  
        factory = Persistence.createEntityManagerFactory("mysqlJPA");  
    }
 
    @AfterClass  
    public static void tearDownAfterClass() throws Exception {  
        factory.close();  
    }
 
    @Before
    public void setUp() {
        this.em = factory.createEntityManager();  
        em.getTransaction().begin();
        removeAll();
    }
 
    @After
    public void tearDown() {
        removeAll();
        em.getTransaction().commit();  
        em.close();  
    }
 
    public void removeAll() {
        Query query = em.createQuery("delete from Person");
        query.executeUpdate();
    }
 
    @Test public void createTable(){  
        //可以验证生成表是否正确
        // moved to setUpBeforeClass
    }  
 
    //new 、托管、脱管、删除      
    @Test public void save(){  
        Person person = new Person(); //person为new状态  
        person.setName("a person");  
        em.persist(person); //持久化实体  
    }
 
    public Person prepareOne() {
        Person person = new Person(); //person为new状态  
        person.setName("a person");  
        em.persist(person); //持久化实体
        return person;
    }
 
    @Test public void update(){  
        Person person = em.find(Person.class, prepareOne().getId());  
        person.setName("new name"); //person为托管状态  
    }  
 
    @Test public void update2(){  
        Person person = em.find(Person.class, prepareOne().getId());  
        em.clear(); //把实体管理器中的所有实体变为脱管状态  
        person.setName("new name2");  
        em.merge(person); //把脱管状态变为托管状态,merge可以自动选择insert or update 数据  
    }  
 
    @Test public void remove(){  
        Person personToDelete = em.find(Person.class, prepareOne().getId());  
        em.remove(personToDelete); //删除实体  
    }  
 
    @Test public void find(){  
        Person person = em.find(Person.class, prepareOne().getId()); //类似于hibernate的get方法,没找到数据时,返回null  
        System.out.println(person.getName());  
    }  
 
    @Test public void find2(){  
        Person person = em.getReference(Person.class, prepareOne().getId()); //类似于hibernate的load方法,延迟加载.没相应数据时会出现异常  
        System.out.println(person.getName()); //真正调用时才查找数据  
    }  
}

下面是Entity和jpa xml,就是原文的例子,没有修改。
Person.java

package com.jpa.example;
 
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;  
    }  
 
}

META-INF/persistence.xml

<persistence version="2.0"
    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_2_0.xsd">
 
    <persistence-unit name="mysqlJPA" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
 
        <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="" />  
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/sfshowcase" />  
            <property name="hibernate.max_fetch_depth" value="3" />  
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

本页面的文字允许在知识共享 署名-相同方式共享 3.0协议和GNU自由文档许可证下修改和再使用,仅有一个特殊要求,请用链接方式注明文章引用出处及作者。请协助维护作者合法权益。


系列文章

文章列表

  • Java JPA的测试

这篇文章对你有帮助吗,投个票吧?

rating: 0+x

留下你的评论

Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License