尝试将JPA与Derby一起使用时ClassNotFoundException:org.apache.derby.jdbc.EmbeddedDriver

时间:2009-12-12 20:24:27

标签: java jpa weblogic derby weblogic-10.x

我问的相关查询:How to configure & package a simple Java app to use JPA

我有一个简单的Derby数据库,我可以通过WebLogic中托管的EJB连接到该数据库。现在我想使用JPA从命令行Java应用程序连接到它。

到目前为止,这个Java代码+ persistence.xml文件引发了以下错误。我做错了什么?

我的目录布局:

C:.
│   Registrar.class
│   Registrar.java
│   TxnClient.class
│   TxnClient.java
│
└───META-INF
        persistence.xml

独立客户端:

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&gt; 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 

2 个答案:

答案 0 :(得分:3)

要使用未随WebLogic Server一起安装的第三方JDBC驱动程序,您需要更新WebLogic Server的CLASSPATH以包含JDBC驱动程序类的位置。编辑commEnv.cmd/sh中的WL_HOME/common/bin脚本并添加您的课程。

这适用于Derby的JDBC驱动程序(请参阅third party driver)。如果您不知道将JAR放在何处,请将其与WL_HOME\server\lib一起放在与Weblogic捆绑在一起的其他JDBC驱动程序中。

PS:我想知道你为什么不使用Kodo,这是WebLogic的默认持久性引擎,但这是另一个故事。

答案 1 :(得分:2)

ClassNotFoundException表示相关的类(org.apache.derby.jdbc.EmbeddedDrive)不在您的类路径中。把它(德比罐)放在那里再试一次。