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.
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
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.
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,
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.
Put the configuration in src/test/resources/META-INF
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With