Hibernate can't find the H2 driver

First of all sorry for my English

I have a Maven project with Hibernate and JPA. I have already annotated the 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:

<?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:

<?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?

1 answer

  • answered 2018-10-09 17:35 resatz

    In the above pom

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

    Remove the <scope>test</scope>