A related query I asked: How to configure & package a simple Java app to use JPA
I have a simple Derby database which I can connect to via EJBs hosted in WebLogic. Now I would like to connect to it from a command line Java app using JPA.
So far this Java code + persistence.xml file is throwing the error below. What am I doing wrong?
My directory layout:
C:.
│ Registrar.class
│ Registrar.java
│ TxnClient.class
│ TxnClient.java
│
└───META-INF
persistence.xml
Standalone client:
import javax.persistence.*;
public class TxnClient {
public static void main(String[] args) throws Exception {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("SRS-EM");
EntityManager manager = factory.createEntityManager( );
try {
} finally {
manager.close( );
factory.close( );
}
}
}
persistence.xml
<?xml version="1.0" encoding="windows-1252" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="SRS-EM" transaction-type="JTA">
<class>Registrar</class>
<properties>
<property name="openjpa.ConnectionDriverName"
value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="openjpa.ConnectionURL"
value="jdbc:derby:C:\derby-db-files\MyDB" />
</properties>
</persistence-unit>
</persistence>
C:\\temp\\jpa>java TxnClient
80 SRS-EM INFO [main] openjpa.Runtime - Starting BEA Kodo 4.2.0load03
221 SRS-EM INFO [main] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.DerbyDictionary".
Exception in thread "main" <openjpa-1.1.0-r422266:657916 nonfatal general error>
org.apache.openjpa.persistence.PersistenceException: There were errors initializing your configuration: <openjpa-1.1.0-r422266:657916 fatal store error> org.apache.openjpa.util.StoreException: org.apache.derby.jdbc.EmbeddedDriver
at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:123)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:776)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getConnectionFactory(JDBCConfigurationImpl.java:683)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.openjpa.lib.conf.ConfigurationImpl.instantiateAll(ConfigurationImpl.java:288)
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.instantiateAll(OpenJPAConfigurationImpl.java:1409)
at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:646)
at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:183)
at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:142)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:192)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:145)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:56)
at TxnClient.main(TxnClient.java:7)
Caused by: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:85)
... 15 more
To use a third-party JDBC driver that is not installed with WebLogic Server, you need to update the WebLogic Server's CLASSPATH
to include the location of the JDBC driver classes. Edit the commEnv.cmd/sh
script in WL_HOME/common/bin
and prepend your classes.
This applies to the JDBC driver for Derby (see third party driver ). If you don't know where to put the JAR, put it in WL_HOME\\server\\lib
with other JDBC drivers bundled with Weblogic.
PS: I wonder why you're not using Kodo which is the default persistence engine of WebLogic but this is another story.
ClassNotFoundException
indicates that the class in question ( org.apache.derby.jdbc.EmbeddedDrive
) is not on your classpath. Put it (the derby jar) there and try again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.