javax.persistence.PersistenceException: No Persistence provider for EntityManager named XX [duplicate] - java

This question already has answers here:
No Persistence provider for EntityManager named
(32 answers)
Closed 9 years ago.
I have a simple JPA/Hibernate Project in Eclipse (Helios) IDE.
I have placed the persistence.xml in Project>resources dir.
And in Eclipse i have added this directory(Project\resources) to the classpath.
Yet when i run my appln frm within Eclipse IDE, i see this error:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named XX
In Eclipse, i've following Libraries:
hibernate3.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate-entitymanager-3.6.0.Final.jar
and rest are supporting jars...
My persistence.xml is:
<?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="XX" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- the JNDI data source -->
<non-jta-data-source>java:comp/env/jdbc/tutorialDS</non-jta-data-source>
<properties>
...
</properties>
</persistence-unit>
</persistence>
What is causing this error in Eclipse IDE?
Thanks

So the solution:
In Eclipse > Properties > Java Build Path
Source Tab - there was: Excluded: META-INF/
This was causing the META-INF dir to be completely ignored, and persistence.xml was not found.
On deleting that Excluded:META-INF/ line, persistence.xml is now found successfully.
How did- Excluded: META-INF/ end up in Java Build Path > 'Source' tab?
Earlier under the 'Libraries' Tab, i had done: 'Add Class Folder' : src/META-INF
This automatically resulted in Excluded: META-INF/ under 'Source' tab.
Hope this helps someone.

The persistence.xml must be placed inside META-INF directory.
Edit: don't forget include the library of the driver for your database.

Have you tried to keep this persistence.xml file into class path META-INF folder.
{CLASS_PATH}/META-INF/persistence.xml

Related

Where to put -ds.xml?

I am having trouble adding a datasource to my wildfly server using a -ds.xml file as recommended by the jboss doc. I have a maven project with a persistence.xml:
<?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="primary">
<jta-data-source>java:jboss/datasources/DvdRental4</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
and a postgres-ds.xml in the root of the project:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>DvdRental4</jndi-name>
<connection-url>jdbc:postgresql://localhost:5432/dvdrental4</connection-url>
<driver>postgresql-9.4.1208</driver>
<user-name>postgres</user-name>
<password>1234</password>
<metadata>
<type-mapping>PostgreSQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
When running mvn package and deploying the .war, I get an error that DvdRental4 is missing. So I suppose I need to place the postgres-ds.xml somewhere specific, but where? Or maybe the xml files are wrong? If needed, I can also provide the pom.xml. I already created the database dvdrental4, so this shouldn't be an issue.
I also read somewhere that you have to place the -ds.xml file in the deploy folder, I only have a wildfly/standalone/deployments folder, and putting it in there didn't work.
EDIT:
Wildfly gives the following error:
ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 2) WFLYCTL0013: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {
"WFLYCTL0412: Required services that are not installed:" => ["jboss.naming.context.java.jboss.datasources.DvdRental4"],
"WFLYCTL0180: Services with missing/unavailable dependencies" => [
"jboss.persistenceunit.\"dvdrental-gruppe-4.war#primary\".__FIRST_PHASE__ is missing [jboss.naming.context.java.jboss.datasources.DvdRental4]",
"jboss.persistenceunit.\"dvdrental-gruppe-4.war#primary\" is missing [jboss.naming.context.java.jboss.datasources.DvdRental4]"
]
}
I think I solved it by just putting the *-ds.xml file in the project/src/main/webapp/WEB-INF of my maven project, at least there is no error saying the datasource couldn't be found. I also changed the postgres-ds.xml a bit:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<datasource jndi-name="java:jboss/datasources/DvdRental4" pool-name="DvdRental4"
enabled="true" use-java-context="true">
<connection-url>jdbc:postgresql://localhost:5432/dvdrental4</connection-url>
<driver>postgresql-9.4.1208.jar</driver>
<security>
<user-name>postgres</user-name>
<password>1234</password>
</security>
</datasource>
</datasources>
Note: I deployed the driver as a .jar, so that is why I can specify the driver directly as a .jar, I think.
The JNDI name you specified for your datasource is "DvdRental4". But the persistence context specifies "java:jboss/datasources/DvdRental4". I am not sure whether that is correct. Either try "java:jboss/datasources/DvdRental4" in both places or (even better) use the Boss JNDI view to see where the datasource is actually deployed.
The ability to deploy datasources was not a feature that made it between JBoss 5 and the AS7 rewrite, only the deployable jms destinations. You will need to configure the server to define the datasource, before you deploy your application

why getting “No Persistence provider for EntityManager” when running with jnlp while standalone jar works fine

Environment
Win 8.1
MySQL server: localhost
Netbeans 7.4
Java: 1.7.0_51 (J2SE)
JRE: 1.7.0_51-b31
EclipseLink(JPA 2.1)
There are several posts asking about this “no persistence provider for EntityManager named…” error. I am developing with J2SE (not J2EE). However, my problems seem silly but it really troubles me for 2days. Is it that I need some other software for this standalone program to work? But why is it oaky when I run the program under Netbeans IDE? Is it some sort of environment setting issue which I failed to try?
Working snapshot and EclipseLink message!
I use Netbeans to write code, and it runs okay. Now, I decided to copy the whole standalone package out “D:\NetBeansWork\ProjCostTracking\dist”. I have change the security setting to medium in Java Control Panel. Then, I double-clicked on ProjCostTracking.jnlp to launch. Well, this is what I see.
this is my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
<persistence-unit name="ProjCostTrackingPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>ProjCostTracking.EntityUserlevel</class>
<class>ProjCostTracking.EntityUser</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/biotechcost?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.password" value="1234"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
</properties>
</persistence-unit>
</persistence>
The entity files are
The generated files and lib
As for persistence.xml, I copy to 3 places just to make sure it can be “seen”.
As for CLASSPATH, I copied the required jar files (from ProjCostTracking\dist\lib) to lib folder under my default JDK and JRE. So java suppose to see the jars.
Usually this is a connector issue.
It seems that eclipse is not using the right connector. Check the connector version. It should match with the version of your MySQL database.
I guess I am using JNLP in a wrong way. Because when I run the standalone with "java -jar myprogram.jar" it works perfectly, no error. But, when I use jnlp it gives me the error. Hope I am not misleading everyone here. It is more related to how jnlp works with the persistence stuff.
The Problem is that if you want to use Application Managed Entity Manager you have to control the scope of the drivers and used APIs.
For example: in a Java SE Application you can build the project with maven. The Scope of the dependencies signals when the API or the package has to be used. E.g.
(...)
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.1.Final</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.1.Final</version>
<scope>runtime</scope>
</dependency>
(...)
Because the life circle is managed by the application and not by the container (like in Java EE applications).
In my case it was important to change the scope from provided to runtime (Same with the database driver). In your case - if you are not using maven you are dependent what the JVM is using.
Maybe this is helpful. I know this kind of issues and it is really annoying :|
Maven Scope Doc

No Persistence provider for EntityManager named X

I am developing a JavaSE application using JPA. Unfortunately, I get null after calling:
Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
Below you will find:
A snippet of my code that invokes EntityManagerFactory and unexpectedly returns null
My persistence.xml file
My project structure
Snippet of my code:
public class Main {
private static final String PERSISTENCE_UNIT_NAME = "MeineJpaPU";
private static EntityManagerFactory factory;
public static void main(String[] args) {
// I get null on this line!!!
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
// do stuff with entity manager
...
}
}
My persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="MeineJpaPU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>path.to.package.server.Todo</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="postgres"/>
</properties>
</persistence-unit>
</persistence>
My project structure:
You must move persistence.xml file to an appropriate location.
More specifically, add META-INF/persistence.xml file to the root of a source folder.
In this case, the following is an appropriate location: src\main\java\META-INF\persistence.xml
Here are the details:
(taken from the 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
Quick advice:
check if persistence.xml is in your classpath
check if hibernate provider is in your classpath
With using JPA in standalone application (outside of JavaEE), a persistence provider needs to be specified somewhere. This can be done in two ways that I know of:
either add provider element into the persistence unit: <provider>org.hibernate.ejb.HibernatePersistence</provider> (as described in correct answer by Chris here: https://stackoverflow.com/a/1285436/784594)
or provider for interface javax.persistence.spi.PersistenceProvider must be specified as a service, see here: http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html (this is usually included when you include hibernate,or another JPA implementation, into your classpath
In my case, I found out that due to maven misconfiguration, hibernate-entitymanager.jar was not included as a dependency, even if it was a transient dependency of other module.
See also answers here: No Persistence provider for EntityManager named
So in my case, everything was in the class path, but I had to add
Class c = Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
I think this caused the PersistenceProvider to register itself with the javax classes. I have had to do something similar for JDBC drivers in the past as well.
I recently upgraded NB 8.1 to 8.2 and suddenly faced this problem and spent 2 days breaking my head to resolve. Up to 8.1, removing processorpath (mentioned above by others) worked. With 8.2, the problem persisted.
Finally, I found that the eclipselink.jar is missing in the default library of EclipseLink (JPA 2.1). I added the file to the definition of the library and voila - it started working!
If you don't want to move your META-INF folder (maybe because that's where your IDE created it and you don't want to confuse it), and if you are using Maven, you can tell Maven where to look for META-INF using the <resources>tag. See: http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html
In your case:
<build>
<resources>
<resource>
<directory>src</directory>
</resource>
</resources>
</build>
Dont give JPA dependency explicity
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
Thanks,
Rahul

Weblogic Server Upgrade: EJB3 JPA KODO: No persistence unit named 'em' is available in scope

I have been wrestling with this error for last few days. Please someone point out what is going wrong.
I have an EAR application in production on Weblogic Server 10.0 working just fine. When I deploy the same build EAR on Weblogic Server 10.3.4, I get an error:
No persistence unit named 'em' is available in scope /projectEARName. Available persistence units: []
The application is an exploded EAR with exploded JAR and WAR. The JAR uses JPA (Kodo framework, which is the JPA RI in WebLogic) and EJB3. WAR uses JSF Framework.
Project Final Build Folder Structure (Build using ANT; All Exploded; Which get deployed in WLS)
EAR
JAR
Project's Classes
META-INF
persistence.xml
ejb-jar.xml
weblogic-ejb-jar.xml
war
Project's UI Files
WEB-INF
lib
classes
META-INF
weblogic-application.xml
application.xml
persistence.xml
<?xml version="1.0" encoding="UTF-8"?><persistence version="1.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_1_0.xsd">
<persistence-unit name="PU" transaction-type="JTA">
<provider>kodo.persistence.PersistenceProviderImpl</provider>
<jta-data-source>SMDS</jta-data-source>
<class>com.aaa.bbbb.domainobjects.cccc</class>
<class>com.aaa.bbbb.domainobjects.dddd</class>
<class>com.aaa.bbbb.domainobjects.eeee</class>
<properties>
<property name="kodo.jdbc.SQLFactory" value="BatchLimit=-1, SupportsUnion=true"/>
<property name="kodo.DataCache" value="true(CacheSize=5000, SoftReferenceSize=0)"/>
<property name="kodo.RemoteCommitProvider" value="sjvm"/>
<property name="kodo.Log" value="DefaultLevel=INFO, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
</properties>
</persistence-unit>
EJB Facade Class (which uses EntityManager)
#Stateless(name="XYZFacade", mappedName="XYZFacade")
public class XYZFacadeBean implements XYZFacade {
#PersistenceContext
private EntityManager em;
}
I think the issue might be due to the change in JARs in modules folder in Weblogic installation folder. Any ideas, anyone ?

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/sushi

I have a web application using a jar file(lib) to access a Database.
The jar file when used as a standalone application executes correctly but the webapp received the error:
[01-08-12 13:17:05] - 35266 WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 08001
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/myapp
I have read the answers to similar questions but none of them solve my problem.
I am using Maven, where I have added the dependency for the mysql version I am using, 5.1.21. In fact, I have added it for both the Lib and the Webapp.
Before that, I tried to define in Eclipse a Connectivity Driver Definition bound to the same file, that I had copied to WEB-INF/lib in the eclipse project for my Webapp and with the same parameters that I include below for the persistence.xml file
I am not using any java to configure since I do all the configuration in the persistence.xml file(that I have copied to both META-INF folders, the one for the app(lib) and the one for the webapp. I am using Hibernate (4.1.2) through JPA.
The persistence.xml file is like that:
<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_2_0.xsd"
version="2.0">
<persistence-unit name="myappPersistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider> org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myapp" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
</properties>
</persistence-unit>
</persistence>
the exception that I get is at Runtime, when launching an Http Request to the app, not at the initialization of the webapp, where I haven't seen any errors but this warning appears:
[01-08-12 13:16:39] - 8782 WARN
org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342:
Could not obtain connection to query metadata : No suitable driver
found for jdbc:mysql://localhost:3306/myapp
I guess this is due to the information being looked up from the database is not necessary at init, and no exception is thrown but the same root cause in both cases.
EDIT:I am using Tomcat 6.0
Any help will be highly appreciated.
It seems that you are missing MySql driver in your project. To solve this you need to add mysql-connector.jar. externally to your project.
Or if you are using a maven project then use the following dependency in your pom.xml file.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.4</version>
</dependency>
You can use a suitable version if needed!!
If you're running your application as a Java Application, Add the JAR file in the Java Build Path, in Eclipse.
Alternatively, I wouldn't put the MSQL jar inside WEB-INF/lib folder of your web project, instead, I would put it in your Application folder library folder, where the Application Server will load your driver and you can access it from the Application Server container.
you should put mysql connector jar with your own code & add jar file with your project.
I also had the same problem some time before, but I solved that issue. There may be different reasons for this exception.
One of them may be that the jar you are adding to your lib folder may be old. Try to find out the latest mysql-connector-jar version and add that to your classpath. It may solve your issue.
I had the same problem with Tomcat 9. Solved by not having the driver jar in both common/lib and WEB-INF/lib (used the former).

Categories

Resources