我问的一个相关查询: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( );
}
}
}
持久性.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