eclipse 如何通过eclipse在JUnit测试中使用persistence.xml和hibernate.cfg.xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12885295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:12:03  来源:igfitidea点击:

How to use persistence.xml and hibernate.cfg.xml in JUnit tests via eclipse?

eclipsehibernatejpajunitjpa-2.0

提问by fabien7474

I have been searching for an answer during quite a long time before coming here and ask this question. My problem is very simple : I cannot run any JUnit test when using the JPA Entity Manager API. It seems that my test is not reading the persistence.xmlfile.

在来这里问这个问题之前,我一直在寻找答案很长一段时间。我的问题很简单:使用 JPA 实体管理器 API 时我无法运行任何 JUnit 测试。看来我的测试没有读取persistence.xml文件。

When I call new Configuration().configure().buildSessionFactory(), it works like a charm but an exception is thrown when calling Persistence.createEntityManagerFactory("myapp")is:

当我调用时new Configuration().configure().buildSessionFactory(),它的作用就像一个魅力,但调用时会引发异常Persistence.createEntityManagerFactory("myapp")

javax.persistence.PersistenceException: No Persistence provider for EntityManager named myapp at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) at com.myapp.Test01.setUpBeforeClass(Test01.java:22)

javax.persistence.PersistenceException:在 com.myapp.Test01 的 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) 的 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69) 的名为 myapp 的 EntityManager 没有持久性提供程序。 setUpBeforeClass(Test01.java:22)

My JavaSE project has the following structure :

我的 JavaSE 项目具有以下结构:

myapp
++src/main/java
   ++com.myapp.model
      ++// My entity classes are here
++src/test/java
   ++com.myapp
      ++Test01.java
++src/main/resources
  ++hibernate.cfg.xml
  ++META-INF 
    ++persistence.xml

and persistence.xmllooks like:

persistence.xml看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    version="2.0">
    <persistence-unit name="myapp" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"></property> 
        </properties>
    </persistence-unit>
</persistence>

Do you have any idea how to solve this issue ?

你知道如何解决这个问题吗?

Thank you very much for your help.

非常感谢您的帮助。

回答by s.froehlich

Put the configuration in src/test/resources/META-INF.

将配置放入src/test/resources/META-INF.

回答by Tom Anderson

My guess would be that org.hibernate.ejb.HibernatePersistenceis not on your classpath. That's not in the main Hibernate jar, it's in the hibernate-entitymanager or some such jar.

我的猜测是这org.hibernate.ejb.HibernatePersistence不在您的类路径中。那不是在主 Hibernate jar 中,而是在 hibernate-entitymanager 或一些此类 jar 中。

A couple of quick experiments would be to do, in the code immediately before the call to Persistence.createEntityManagerFactory():

在调用 之前的代码中,可以进行一些快速实验Persistence.createEntityManagerFactory()

System.out.println(getClass().getResource("META-INF/persistence.xml"));

And:

和:

System.out.println(Class.forName("org.hibernate.ejb.HibernatePersistence"));

It would also be wise to check that Eclipse is copying your resources; do a Project > Clean and then look in your Eclipse build output directory to see if persistence.xml is where you expect. I have sometimes run into cases where Eclipse doesn't copy resources for some reason or other.

检查 Eclipse 是否正在复制您的资源也是明智之举;执行 Project > Clean,然后查看 Eclipse 构建输出目录,看看是否persistence.xml 是您期望的位置。我有时会遇到 Eclipse 由于某种原因不复制资源的情况。