简体   繁体   English

Hibernate 找不到 H2 驱动程序

[英]Hibernate can't find the H2 driver

First of all sorry for my English首先对不起我的英语

I have a Maven project with Hibernate and JPA.我有一个带有 Hibernate 和 JPA 的 Maven 项目。 I have already annotated the enitites.我已经注释了enitites。 When I try to run my program it shows this error:当我尝试运行我的程序时,它显示此错误:

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

This is my hibernate.cfg.xml:这是我的 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>

And this is my pom.xml:这是我的 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>

How can I fix this problem?我该如何解决这个问题?

In the above pom 在上面的pom

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

Remove the <scope>test</scope> 删除<scope>test</scope>

For Gradle: testImplementation 'com.h2database:h2:1.4.200' => will be given by default use implementation 'com.h2database:h2:1.4.200' in dependencies task对于 Gradle: testImplementation 'com.h2database:h2:1.4.200' => 默认情况下会在依赖项任务中使用实现 'com.h2database:h2:1.4.200'

if we don't remove test then it will be taken in test module only如果我们不删除测试,那么它将仅在测试模块中进行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM