This question already has answers here:
No Persistence provider for EntityManager named X
(6 answers)
Closed 9 years ago.
My persistence xml file is like that
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="hibernateEbru">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.hibernate.business_card</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="2643" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/BusinessDb" />
</properties>
</persistence-unit>
</persistence>
Then I have my code calling it with this:
public class test {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hibernateEbru");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
business_card bc = new business_card();
bc.setName("Ebru");
em.persist(bc);
em.getTransaction().commit();
em.close();
emf.close();
}
}
I got the following error message:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named hibernateEbru
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
at com.hibernate.test.main(test.java:8)
You will need to move the persistence.xml file to an appropriate location
From JPA spec:
A persistence.xml file defines a persistence unit. The persistence.xml
file is located in the META-INF directory of the root of the
persistence unit.
The root of the persistence unit is the key here.
If you are a non-Java EE app
The jar file or directory whose META-INF directory contains the
persistence.xml file is termed the root of the persistence unit.
If you are in a Java EE app, the following are valid
In Java EE environments, the root of a persistence unit must be one of
the following:
an EJB-JAR file
the WEB-INF/classes directory of a WAR file[80]
a jar file in the WEB-INF/lib directory of a WAR file
a jar file in the EAR library directory
an application client jar file
Related
Getting the error "No Persistence provider for EntityManager named" while trying to run Java Maven project in IntelliJ, the project runs fine in Visual studio Code with no errors or warnings
i'm not fully sure what code be causing the issue
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named default
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at JNWR.ServerApplication.<clinit>(ServerApplication.java:18)
here is my persistence file
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
version="2.2">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<description>
Persistence unit for the JNWR Database
</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>JNWR.Entity.Customer</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/jwrms_db" />
<property name="javax.persistence.jdbc.user"
value="defaultUser"/>
<property name="javax.persistence.jdbc.password"
value="defaultPassword%"/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL8Dialect" />
<property name="hibernate.show_sql"
value="true" />
</properties>
</persistence-unit>
</persistence>
Already tried:
Adding META-INF to classpath manually,
moved it into the root folder,
moved it into resources folder,
moved persistence.xml to root, resources and of course META-INF.
Imported the project into vscode (which worked but I want it to work in IntelliJ)
I am trying to learn JPA with Hibernate implementation. There are lot of blog of net about this but still i am struggling to implement it. I have written one Dao class which creates the EntityManagerFactory. But while looking this PERSISTENCE_UNIT it says
javax.persistence.PersistenceException: No Persistence provider for EntityManager named test
private final String PERSISTENCE_UNIT = "test";
private EntityManager entityManager;
public GenericDao() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory(PERSISTENCE_UNIT);
entityManager = factory.createEntityManager();
}
I also created one persistence.xml file which is in META-INF folder of web application. It looks like this.
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgres://localhost:1532/test" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
For deployment Tomcat8 is being used. Please let me know why application not able to lookup PERSISTENCE_UNIT.
As per comment - the problem is your persistence.xml location. It needs to be exactly where your application expects it to be:
WEB-INF/classes/META-INF/persistence.xml
Now, why did you encounter this problem is another matter, connected with how you create your .war file. Solution that should work however, is trusting Maven to take care of that. If you set packaging to war, and put your META-INF folder into resources (src/main/resources/META-INF/persistence.xml) it should take care of things.
I would like to take advantage of JPA #Entity annotations not to declare class entities a J2SE persistence.xml file. What I'd like to avoid :
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.mycompany.entities.Class1</class>
<class>com.mycompany.entities.Class2</class>
<class>com.mycompany.entities.Class3</class>
</persistence-unit>
and here is what my actual persistence.xml look alike
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="hibernate.cache.use_second_level_cache" value="false" />
<property name="hibernate.cache.use_query_cache" value="false" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
Is there a standard way to scan JPA entities in a persistence.xml file from within a JAR module?
Is there a unstandard Hibernate way to scan JPA entities in a persistence.xml file from within a JAR module?
-Make sure your entities and persistence.xml end up in the same classpath when you build your jar.
The entities cannot be scanned if they are sitting in another classpath. If you need to have them sitting in different classpaths, heres one trick I've seen to make it work: No autodetection of JPA Entities in maven-verify.
If you are unsure where things are ending, you can unzip the .jar file and peak. This is an unpacked persistence web project:
Notice my classes are down the com directory, and my persistence.xml is in the META-INF directory. Both are in the same 'classes' classpath, so autoscan will work.
-Set hibernate.archive.autodetection property.
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm" />
-Add false to the persistence-unit
<persistence-unit name=...>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
...
Hopefully one of those will work for you.
I've just migrated from Tomcat to JBoss AS 7.
So, I configured Mysql datasource in JBoss (adding module.xml with associated Jar, adding driver bloc into standalone.xml and configuring datasource through JBoss interface.
No errors when deploying but impossible to get an entityManager (JPA with Hibernate in background).
Indeed, when this code is executed:
Persistence.createEntityManagerFactory("RoomManagement");
I obtain this error :
javax.persistence.PersistenceException: No Persistence provider for
EntityManager named RoomManagement
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
Very strange because I well verified that my persistence.xml does take place into War at WEB-INF/classes/META-INF directory.
My persistence.xml looks like as follow :
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="RoomManagement" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
<class>com.parisdescartes.roommanagement.domain.entities.Address</class>
<class>com.parisdescartes.roommanagement.domain.entities.Building</class>
<class>com.parisdescartes.roommanagement.domain.entities.Civility</class>
<class>com.parisdescartes.roommanagement.domain.entities.EventType</class>
<class>com.parisdescartes.roommanagement.domain.entities.Job</class>
<class>com.parisdescartes.roommanagement.domain.entities.Reservation</class>
<class>com.parisdescartes.roommanagement.domain.entities.Room</class>
<class>com.parisdescartes.roommanagement.domain.entities.RoomType</class>
<class>com.parisdescartes.roommanagement.domain.entities.Tool</class>
<class>com.parisdescartes.roommanagement.domain.entities.User</class>
<class>com.parisdescartes.roommanagement.domain.entities.UserDetail</class>
<class>com.parisdescartes.roommanagement.domain.entities.Schedule</class>
<properties>
<property name="hibernate.connection.autocommit" value="true" />
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
Did I make a mistake or forgot to specify something ?
Remove the hibernate jar from WEB-INF/lib. JBoss has that bundled, so if you have it on the classpath it probably confuses the classloader.
I am using MySql database with JPA. my persistence.xml looks like
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="citheph">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>persistence.citheph.pTerm</class>
<class>persistence.citheph.pPublicationterm</class>
<class>persistence.citheph.pPublicationauthor</class>
<class>persistence.citheph.pPublication</class>
<class>persistence.citheph.pCoauthorship</class>
<class>persistence.citheph.pCitation</class>
<class>persistence.citheph.pAuthor</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cithepth" />
<property name="javax.persistence.jdbc.password" value="amer1" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
</properties>
</persistence-unit>
</persistence>
in my java code i do the following
EntityManagerFactory emf = Persistence.createEntityManagerFactory("citheph");
EntityManager em = emf.createEntityManager();
emf always come back null. the database is connected and pings successfully. I have even tried to change the persistence-unit name to something that doesn't match the entry in the persistance.xml and I am still getting null. It is not throwing any exceptions. ofcourse when it gets to the second line in the code
EntityManager em = emf.createEntityManager();
I get a null pointer exception.
any help will be greatly appreciated.
Turn logging on finest and ensure no errors are occurring loading the persistence unit.
What environment are you using, do you have all the required jars on your classpath?
Are you sure that the persistence.xml files resides in the META-INF folder?