Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

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)

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

and persistence.xml looks like:

<?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.

like image 633
fabien7474 Avatar asked Oct 14 '12 18:10

fabien7474


People also ask

Where do I put JPA persistence XML?

JPA persistence XML file location Traditionally, the persistence.xml is located in a META-INFfolder that needs to reside in the root of the Java classpath. If you’re using Maven, you can store it in the resourcesfolder, like this: src/main/resources/META-INF/persistence.xml JPA persistence XML file structure

Where do I put hibernate persistence files?

Its a JAR file placed inside META-INF folder which must have a persistence.xml file. All the correctly annotated class ( which contains @Entity annotation), all annotated packages and all Hibernate hbm.xml files included in the archive will be added to the persistence unit configuration.

What is persistence XML?

The persistence.xml file defines one or more persistence units, and you can configure things like: the name of each persistence unit, which managed persistence classes are part of a persistence unit, how these classes shall be mapped to database tables,

What is the use of configuration in hibernate?

Hibernate facilitates to provide the configurations either in an XML file (like hibernate.cfg.xml) or properties file (like hibernate.properties). An instance of Configuration class allows specifying properties and mappings to applications. This class also builds an immutable SessionFactory.


2 Answers

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

like image 148
s.froehlich Avatar answered Oct 12 '22 23:10

s.froehlich


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.

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"));

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.

like image 41
Tom Anderson Avatar answered Oct 12 '22 21:10

Tom Anderson