本文介紹了無法使用JPA-DERBY嵌入式驅動程序類獲取到數據庫的連接的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試使用JPA創建本地derby數據庫。作為JPA實現,我使用openjpa和SQL實現derby。
這是sistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
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">
<persistence-unit name="DataLayer"
transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>DataSource</non-jta-data-source>
<properties>
<property
name="javax.persistence.schema-generation.database.action"
value="create" />
<property name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:testdb" />
</properties>
</persistence-unit>
</persistence>
這是我的Main:
public static void main(String[] args)
{
Persistence.generateSchema("DataLayer", null);
EntityManagerFactory factory = Persistence.createEntityManagerFactory("DataLayer");
EntityManager manager = factory.createEntityManager(); // <- Exception here
}
這是我收到的異常的開始部分:
Exception in thread "main" <openjpa-3.1.2-r66d2a72 nonfatal general error> org.a
pache.openjpa.persistence.PersistenceException: There were errors initializing y
our configuration: <openjpa-3.1.2-r66d2a72 fatal user error> org.apache.openjpa.
util.UserException: A connection could not be obtained for driver class "org.apa
che.derby.jdbc.EmbeddedDriver" and URL "jdbc:derby:testdb". You may have spe
cified an invalid URL.
...
說我可能指定了無效的URL。
完成堆棧跟蹤:https://justpaste.it/3kmgo
根異常:java.lang.ClassNotFoundException:org.apache.derby.jdbc.EmbeddedDriver
我已在sistence.xml中指定了EmbeddedDriver,還包括Maven依賴項。
另外,根據有關derby的文檔,我認為我的連接url和驅動程序是正確的:
https://db.apache.org/derby/docs/10.15/devguide/rdevdvlp22102.html#rdevdvlp22102
https://db.apache.org/derby/docs/10.15/devguide/cdevdvlp40653.html
包含的依賴項(使用Maven):
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.2.0</version>
</dependency>
我嘗試將屬性javax.persistence.jdbc.url
更改為jdbc:derby:testdb;create=true
,但出現相同的異常。我還嘗試將javax.persistence.schema-generation.database.action
屬性設置為none
。
推薦答案
發現org.apache.derby.jdbc.EmbeddedDriver
類不在derby
artifactid依賴項中(它有derby和derbyshare jar)。它在derbytools中,因此包含derbytools來解決關于缺少EmbeddedDriver
類問題:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</artifactId>
<version>10.15.2.0</version>
</dependency>
這篇關于無法使用JPA-DERBY嵌入式驅動程序類獲取到數據庫的連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,