JPA 单元测试配置

因为项目是用maven创建的,所以JPA配置文件应放在:test/resources/META-INF/persistence.xml

<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_2_0.xsd
            http://java.sun.com/xml/ns/persistence "
             version="2.0">
    <persistence-unit name="unit.test" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver"/>
            <property name="hibernate.connection.url" value="jdbc:derby:database;create=true"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyTenSevenDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

在单元测试时,在抽象层中应定义几个最简单的增删改查方法:

package org.pan.domain;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Created by panmingzhi on 2015/10/29 0029.
 */
public class AbstractHibernateTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractHibernateTest.class);
    private static EntityManagerFactory entityManagerFactory;

    @BeforeClass
    public static void startUp(){
        try {
            entityManagerFactory = Persistence.createEntityManagerFactory("unit.test");
            LOGGER.info("初始化数据库连接池成功");
        } catch (Exception e) {
            LOGGER.info("初始化数据库连接池失败",e);
        }
    }

    @AfterClass
    public static void end(){
        if(entityManagerFactory != null){
            entityManagerFactory.close();
            LOGGER.info("销毁数据库连接池成功");
        }
    }

    protected void save(Object obj) {
        final EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        try {
            entityManager.persist(obj);
            entityManager.getTransaction().commit();
            LOGGER.info("添加新数据:{} 成功",obj);
        } catch (Exception e){
            throw new RuntimeException("添加新数据:"+obj+" 失败",e);
        }finally {
            entityManager.close();
        }
    }

    protected void remove(Class clz,Long id) {
        final EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        try {
            Object reference = entityManager.getReference(clz, id);
            entityManager.remove(reference);
            entityManager.getTransaction().commit();
            LOGGER.info("删除数据:{} 成功",reference);
        } catch (Exception e){
            throw new RuntimeException("删除新数据:"+clz.getSimpleName()+" 失败 , id: " + id,e);
        }finally {
            entityManager.close();
        }
    }

    protected Object find(Class clz, Long id) {
        final EntityManager entityManager = entityManagerFactory.createEntityManager();
        try {
            return entityManager.find(clz,id);
        } catch (Exception e){
            throw new RuntimeException("查找数据:"+clz.getSimpleName()+" , id: " + id + " 失败",e);
        }finally {
            entityManager.close();
        }
    }

}


 

你可能感兴趣的:(jpa)