OpenJPA - ArgumentException: JDBC driver or data source class name - java

I'm creating an OpenJPA application with maven.
The application starts, but if I try to access a database-entity, I get the following exception:
HTTP Status 500 - <openjpa-2.2.2-r422266:1468616 fatal user error> org.apache.openjpa.persistence.ArgumentException: The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database onnectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl#442ce698".
The persistence.xml is located in META-INF and is under control by Netbeans 8.
The ConnectionDriverName is set as the following:
<property name="openjpa.ConnectionDriverName" value="org.apache.commons.dbcp.BasicDataSource"/>
My src\main\java\META-INF\persistence.xml (class-tags reduced and database-settings changed):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="PersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.example.test.my.entities.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
<property name="openjpa.Log" value="DefaultLevel=INFO, Tool=INFO, SQL=TRACE"/>
<property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.MySQLDictionary"/>
<property name="openjpa.ConnectionDriverName" value="org.apache.commons.dbcp.BasicDataSource"/>
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=80, PrintParameters=true"/>
<property name="openjpa.ConnectionProperties" value="DriverClassName=com.mysql.jdbc.Driver,
Url=jdbc:mysql://localhost:3306/database?autoReconnect=true,
Username=usr,
Password=pwd,
MaxWait=60000,
TestOnBorrow=true,
validationQuery=select 1"/>
</properties>
</persistence-unit>
</persistence>
I'm using Tomcat 8 (same result for Tomcat 7 or Tomee).

solved it by switching to eclipse, because it seems like netbeans having some problems with the persistence.xml.
In eclipse I had to use the following m2e-connector:
https://github.com/hwellmann/m2eclipse-extras/raw/master/p2/ (eclipse software repository url)

Related

How to deal with "No Persistence provider for EntityManager named" error?

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)

Read configuration parameters from Wildfly system (standalone.xml)

I am migrating a web application from Tomcat to Wildfly.
The application was using a configuration file stored under tomcat.
I want to avoid having configuration files and point the application to read these files.
I am looking to have the configuration read from Wildfly system like standalone.xml for instance.
what is the best approach?
Thanks
Please use persistence.xml file in Java app for definition make connection to database from standalone.xml file from WildFly
Excample persistence.xml - connection is get from WildFly. Please add JNDI name java:/PostgresDS in WildFly
<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_1.xsd"
version="2.1">
<persistence-unit name="em1">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/PostgresDS</jta-data-source>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>

Java Web Application with Kundera JPA on Tomcat 7

I have developed a JAVA Rest Service (JDK 1.8) which uses Kundera (V3.2) for a Cassandra Database connection. If I run my application inside eclipse with a Tomcat-Server everything works well. If I want to deploy .war file on another Tomcat 7 the server does not start because of following error:
com.impetus.kundera.loader.MetamodelLoaderException: Error while retreiving and storing entity metadata, Caused by : .
After searching for some solution I found out that the problem can be that I have my entities in an separated jar (which is a dependency in the war file) and and not in the application itself. If this is the problem I have to add the <jar-file> tag to my persistence.xml (https://github.com/impetus-opensource/Kundera/issues/90).
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="cassandra_pu" transaction-type="RESOURCE_LOCAL">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<class>com.asdf.booking.beans.kundera.Account</class>
<class>com.asdf.booking.beans.kundera.AccountCard</class>
<class>com.asdf.booking.beans.kundera.AccountType</class>
<class>com.asdf.booking.beans.kundera.Booking</class>
<class>com.asdf.booking.beans.kundera.Circle</class>
<class>com.asdf.booking.beans.kundera.AccountSequence</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="kundera.nodes" value="1.1.1.1" />
<property name="kundera.port" value="9160" />
<property name="kundera.keyspace" value="booking" />
<property name="kundera.dialect" value="cassandra"/>
<property name="sessionless" value="false" />
<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.thrift.ThriftClientFactory" />
<property name="kundera.annotations.scan.package" value="com.asdf.booking.kundera.beans" />
<property name="kundera.ddl.auto.prepare" value="update" />
</properties>
</persistence-unit>
</persistence>
Is the separate jar-file possibly the problem or should I search for other solutions?
The problem was the separate jar-file for entities. When adding the jar-file tag, pointing to the jar for entities, to the persistence.xml everything is ok.

Why do we have to give database credentials in persistence.xml

We are using JPA 2.0 and we created the datasource in websphere and tried to access the database through the J2SE application. We are getting Invalid Username and password error. If we give the user name and password in persistence.xml it works fine.
Please anybody explain me why do we have to give the DB credentials in persistence.xml since we have the data source.
Note: Data Source was created successfully and the test was success.
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="Printer">
<jta-data-source>jdbc/TestDataSource</jta-data-source>
<properties>
<property name="openjpa.Optimistic" value="false" />
<property name="openjpa.LockManager" value="pessimistic" />
<property name="javax.persistence.jdbc.user" value="admin" />
<property name="javax.persistence.jdbc.password" value="admin#2" />
</properties>
</persistence-unit>
</persistence>
It seems that the datasource is not configured correctly in websphere. Test the database connection through the websphere console to verify the configuration.
A DataSource has 2 methods to get a Connection. It seems that your jpa implementation uses DataSource.getConnection(String username, String password) if you provide the credentials via properties.
The connection properties are intented to use in a Java SE environment. In JEE you should prefer the JNDI lookup. See section 8.2.1.9 of the JPA 2.0 specification.
You don't need to specify the credentials in your application. Just access your database via JNDI, specifying the name of the datasource you've created in the WS.
One way is to configure persistence unit using the pre-configured datasource (Please double check if it is configured correctly - the test feature is available in WS)
<persistence-unit name="default" transaction-type="JTA">
<provider>
oracle.toplink.essentials.PersistenceProvider
</provider>
<jta-data-source>
jdbc/MyDataSource
</jta-data-source>
<properties>
<property name="toplink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
If the datasource is not configured on the application container side, you may set it up yourself on the application side. For example, you need a number of the applications each one has its own database connection config.
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>
oracle.toplink.essentials.PersistenceProvider
</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.logging.level" value="INFO"/>
<property name="toplink.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="toplink.jdbc.url" value="jdbc:oracle:thin:#myhost:l521:MYSID"/>
<property name="toplink.jdbc.password" value="tiger"/>
<property name="toplink.jdbc.user" value="scott"/>
</properties>
</persistence-unit>
Your persistence.xml has the config for both ways. Remove the unnecessary code and try again.

No persistence provider found with JBoss AS 7

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.

Categories