Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有Hibernate和H2的Java1.5JPA创建EntityManagerFactory的速度很慢_Java_Hibernate_Jpa_H2_Java 5 - Fatal编程技术网

带有Hibernate和H2的Java1.5JPA创建EntityManagerFactory的速度很慢

带有Hibernate和H2的Java1.5JPA创建EntityManagerFactory的速度很慢,java,hibernate,jpa,h2,java-5,Java,Hibernate,Jpa,H2,Java 5,我试图在Junit中测试我们的一些DB层 我的问题是Persistence.createEntityManagerFactory()需要超过200秒!谷歌搜索H2、JPA、Hibernate与createEntityManagerFactory的结合并没有提供任何有用的信息 这是我的测试: import static org.junit.Assert.assertTrue; import java.util.Date; import javax.persistence.EntityManag

我试图在Junit中测试我们的一些DB层

我的问题是Persistence.createEntityManagerFactory()需要超过200秒!谷歌搜索H2、JPA、Hibernate与createEntityManagerFactory的结合并没有提供任何有用的信息

这是我的测试:

import static org.junit.Assert.assertTrue;

import java.util.Date;

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

import org.junit.Test;

public class MinimalSet {
    @Test
    public void TestNamedQuery() {
        Date startTime = new Date();

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("TestPersistence");
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        Date endTime = new Date();

        // Did it take less than 10 seconds?
        assertTrue(((endTime.getTime() - startTime.getTime()) / 1000.0) < 10.0);
    }
}
import static org.junit.Assert.assertTrue;
导入java.util.Date;
导入javax.persistence.EntityManager;
导入javax.persistence.EntityManagerFactory;
导入javax.persistence.persistence;
导入org.junit.Test;
公共类极小集{
@试验
公共void TestNamedQuery(){
日期开始时间=新日期();
EntityManagerFactory EntityManagerFactory=Persistence.createEntityManagerFactory(“TestPersistence”);
EntityManager EntityManager=EntityManager工厂。createEntityManager();
日期结束时间=新日期();
//用了不到10秒?
assertTrue(((endTime.getTime()-startTime.getTime())/1000.0)<10.0);
}
}
这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="TestPersistence" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.connection.url" value="jdbc:h2:mem:test" />
            <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="false" />
        </properties>
    </persistence-unit>
</persistence>

org.hibernate.ejb.HibernatePersistence
我不使用任何其他配置文件(例如hibernate.cfg.xml)

我想知道为什么要花这么长时间

谢谢

更新:


它已经开始快速运行(您应该使用
@Test(timeout=10*1000)
设置测试可以持续的最长时间。它更干净、更安全(因为测试可能永远不会结束)而不是获取开始和结束时间并计算差异。如果仍然需要很长时间,您可以尝试使用Eclipselink吗?测试中的应用程序不使用Eclipselink,所以我不能。您使用的是旧版本的Hibernate-不确定但最新版本是4.3.4.Final。我使用此版本运行了您的测试,耗时0.8秒(IMO EclipseLink更快,因为它发现您可以使用模式创建一个h2 db,并在大约这个时间内测试一个小型orm)。