Hibernate找不到H2驱动程序

时间:2018-10-09 16:28:17

标签: java hibernate maven h2

首先对不起我的英语

我有一个Hibernate和JPA的Maven项目。我已经注释了这些贵族。当我尝试运行程序时,它显示此错误:

Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.h2.Driver]
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.h2.Driver

这是我的hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
            <property name="hibernate.connection.driver_class">org.h2.Driver</property>
            <property name="hibernate.connection.url">jdbc:h2:mem:test</property>
            <property name="hibernate.connection.username">sa</property>
            <property name="hibernate.connection.password"></property>
            <property name="hibernate.hbm2ddl.auto">create-drop</property>
            <property name="show_sql">false</property>

            <mapping class="model.User"/>
        </session-factory>
    </hibernate-configuration>

这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>agilexpert</groupId>
    <artifactId>agilexpert</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>      

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.195</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.6.Final</version>
        </dependency>

    </dependencies>

</project>

如何解决此问题?

1 个答案:

答案 0 :(得分:3)

在以上pom

<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.195</version>
        <scope>test</scope>
</dependency>

删除<scope>test</scope>