无法构建实体管理器工厂,无法加载驱动程序

时间:2017-01-23 06:26:46

标签: hibernate jpa mariadb

我正在尝试使用JPA 2.1,Hibernate和MariaDB创建基本的CRUD。

当我尝试在Tomcat上运行我的项目时,我收到此错误:

Caused by: java.lang.ClassNotFoundException: Could not load requested class : mariadb-java-client-1.1.7
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:217)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:226)
... 50 more  

我的数据库已启动并正在运行。我通过maven获取JDBC驱动程序,JAR存在于Maven依赖项文件夹中。这是Persistence.xml(我按照Pro JPA 2.1教科书中的示例):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
<persistence-unit name="CRUD">

    <properties>
        <property name="javax.persistence.jdbc.driver"
                  value="mariadb-java-client-1.1.7"/>
        <property name="javax.persistence.jdbc.url"
                  value="jdbc:derby://localhost:3306/Practice;create=true"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
    </properties>

</persistence-unit>

POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CRUD</groupId>
<artifactId>CRUD</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
        <resource>
            <directory>src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <version>1.1.7</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
</dependencies>

1 个答案:

答案 0 :(得分:2)

您的输入错误。 javax.persistence.jdbc.driver 应该设置为JDBC驱动程序的CLASS NAME,而不是某个jar名称!

<property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver"/>