I keep getting
java.lang.ClassNotFoundException: Could not load requested class : com.ibm.db2.jcc.DB2Driver
when trying to connect to a DB2 database using Hibernate. The driver jar is referenced as an external library:
Image of Eclipse's "Referenced Library" folder
It also shows up in the classpath:
classpathentry kind="lib" path="C:/Program Files (x86)/IBM/SQLLIB/java/db2jcc.jar"/>
I can also access the class by importing it in the source code. My persistence.xml looks as follows:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="name" transaction-type="RESOURCE_LOCAL">
<description>Persistence Unit</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>MyClass</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.ibm.db2.jcc.DB2Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:db2://url" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
My suspicion why this does not work is that the class is contained in an external library and not as a maven dependency, because when I replace the DB2 driver by net.ucanaccess.jdbc.UcanaccessDriver (which is contained in a Maven package), the class will be found just fine.
Any idea what I am doing wrong here?
when you are using com.ibm.db2.jcc.DB2Driver' make sure db2jcc.jar & db2jcc_license_cu.jar are in your classpath.Please add both the jar in your classpath and give a try.
The problem was that I was using the Maven exec plugin to run the main class. W/o using Maven, it works fine...
Related
When I am running my unit test I these two errors
org.hibernate.jpa.boot.internal.PersistenceXmlParser doResolve
INFO: HHH000318: Could not find any META-INF/persistence.xml file in the classpath
and
javax.persistence.PersistenceException: No Persistence provider for EntityManager named dbContext
I don't get them both because as you can see in the image en persistence.xml file below this text everything is there that he is complaining about.
and here you have my persistance.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="dbContext">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="{urltodb}"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.connection.username" value="{myusername}"/>
<property name="hibernate.connection.password" value="{mypassword}"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.CharSet" value="utf8"/>
<property name="hibernate.connection.characterEncoding" value="utf8"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Your folder structure is completely mixed up:
Your src-Folder is marked as source-folder (blue), so the subfolder main is interpreted as Java-Package
Your resources-folder is within your sources/main-folder That's why Intellij interprets it as package main.resources.META-INF.
(All your real packages start with upper-case. That's not an error, but unusual nevertheless)
How your folder structure should look like:
src/main/java => Mark this as source folder and put all your java packages there
src/main/resources => Mark this as resources folder an put your META-INF there
See also: How to create a test directory in Intellij 13?
I am trying to connect to sql server using JPA and hibernate with an Eclipse Maven project. I've tried moving the persistence.xml file to various locations but it doesn't seem to help.
My persistence.xml file is
<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="Trades" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver:/Trades\SQL00;databaseName=Trades" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="dev" />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="show_sql" value="true"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</properties>
</persistence-unit>
</persistence>
and my code is as follows and is located in src/main/resources/META-INF
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Trades");
System.out.println();
}
I have attached a screenshot which shows my eclipse set up
I solved this, turned out that Maven must have downloaded corrupt libraries - I was alerted this when I was unable to view the source for some classes in the Hibernates jars. I deleted the jars in my .m2 directory and when the jars were refreshed, it worked fine. A bit of tricky one.
I have a standalone java application, which uses JPA for its persistence.
Right now I have a persistence.xml in META-INF.My application is currently in development.
My question is that if I move from development to the next envirnoment, say QA. I have to modify the persistence.xml and rebuild the jar. Is this the right way to go about it ?
If not,if I move the connection properties to a different file, where should this file be placed?
<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="pu1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>ClassA</class>
<class>ClassB</class>
<class>ClassC</class>
<class>ClassD</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.username" value="username" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.connection.url"
value="url" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.archive.autodetection" value="class" />
</properties>
</persistence-unit>
</persistence>
Thanks in advance !
That's a good question. Normally, you put all these environment settings in an external file, say application.properties, and pass the location to it to the JVM when you start your application (e.g. -Dconfig.location=/conf/)
Then you should find a way to get the externalized properties into your EntityManagerFactory. You can't do that in persistence.xml, you can only hard-code things there. But you can do it when creating the entity manager factory by passing vendor properties.
If using a framework like spring, for example, this is easier to do, as spring provides a factory bean for the entity manager. Otherwise you should handle it yourself. Here's the relevant bit from spring:
provider.createEntityManagerFactory(persistenceUnitInfo, getJpaPropertyMap())
I've been searching around and haven't been able to find a working fix. My persistence.xml file is located in /src/META-INF/persistence.xml, which from looking around, this is the correct location for it.
I'm using glassfish as the server, and I keep getting the following:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named pers
Here is my persistence file:
<?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="pers" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>cs.ee.assignment2.Client</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/assignment2" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
Any ideas on what the problem could be would be greatly appreciated.
Hmm.. make sure you are using PeristenceUnit not context in your entity class: see http://openejb.apache.org/jpa-concepts.html
Switch to "transaction" just to see if it fails too.
Also sometimes the old classes are not unloaded from the server properly if you are doing redeployment, you may have to shut it down and restart after redeploy.
Hey I'm getting a persistence provide not found exception the code for my persistence.xml is
<?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="GWTSales"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.jeroennouws.sales.beans.Branch</class>
<class>com.jeroennouws.sales.beans.Customer</class>
<class>com.jeroennouws.sales.beans.Employee</class>
<class>com.jeroennouws.sales.beans.Product</class>
<class>com.jeroennouws.sales.beans.Purchase</class>
<class>com.jeroennouws.sales.beans.PurchaseDetail</class>
<class>com.jeroennouws.sales.beans.PurchaseDetailPK</class>
<class>com.jeroennouws.sales.beans.Sale</class>
<class>com.jeroennouws.sales.beans.SalesDesk</class>
<class>com.jeroennouws.sales.beans.SalesDetail</class>
<class>com.jeroennouws.sales.beans.SalesDetailPK</class>
<class>com.jeroennouws.sales.beans.Stock</class>
<class>com.jeroennouws.sales.beans.Supplier</class>
<class>com.jeroennouws.sales.beans.User</class>
<properties>
<property name="eclipselink.jdbc.password" value="" />
<property name="eclipselink.jdbc.user" value="root" />
<property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/sales" />
<property name="eclipselink.logging.level" value="INFO" />
</properties>
</persistence-unit>
</persistence>
and the code that generates this exception is
public BranchJpaController() {
emf = Persistence.createEntityManagerFactory("GWTSales");
}
I have tried all different kinds of things by now, eclipselink jar file is in my WEB-INF/lib folder and my persistence.xml file is in my src/META-INF folder.
Does this has something to do with the GWT Jetty server in the eclipse plugin or am I missing something?
Check if the MySQL JDBC Driver is in your WEB-INF/lib folder.
I hope it helps.