如何通过eclipse在JUnit测试中使用persistence.xml和hibernate.cfg.xml? [英] How to use persistence.xml and hibernate.cfg.xml in JUnit tests via eclipse?

查看:371
本文介绍了如何通过eclipse在JUnit测试中使用persistence.xml和hibernate.cfg.xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.xml file.

当我调用 Persistence.createEntityManagerFactory(myapp)是:


javax.persistence.PersistenceException:否
的持久性提供程序EntityManager命名为myapp
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)

javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
在com.myapp.Test01.setUpBeforeClass( Test01.java:22)

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)

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

My JavaSE project has the following structure :

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

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 ?

非常感谢您的帮助。

推荐答案

我的猜测是 org .hibernate.ejb.HibernatePersistence 不在你的类路径上。这不是主要的Hibernate jar,它是在hibernate-entitymanager或一些这样的jar。

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

几个快速实验将是在紧接在调用 Persistence.createEntityManagerFactory()

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

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

And:

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

检查Eclipse是否正在复制资源也是明智的;做一个项目>清理,然后查看你的Eclipse构建输出目录,看看persistence.xml是否是你期望的地方。有时我会遇到某种情况,Eclipse不会因为某些原因或其他原因复制资源。

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在JUnit测试中使用persistence.xml和hibernate.cfg.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆