无法使用JPA-DERBY嵌入式驱动程序类获取到数据库的连接

Can not obtain connection to DB using JPA - derby embedded driver class missing(无法使用JPA-DERBY嵌入式驱动程序类获取到数据库的连接)

本文介绍了无法使用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类不在derbyartifactid依赖项中(它有derby和derbyshare jar)。它在derbytools中,因此包含derbytools来解决关于缺少EmbeddedDriver类问题:

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbytools</artifactId>
    <version>10.15.2.0</version>
</dependency>

这篇关于无法使用JPA-DERBY嵌入式驱动程序类获取到数据库的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:无法使用JPA-DERBY嵌入式驱动程序类获取到数据库的连接

基础教程推荐