在spring / hibernate中运行H2嵌入式数据库

时间:2017-06-01 01:05:46

标签: java spring hibernate h2

我正在尝试使用hibernate和h2创建一个spring boot应用程序。从我在网上找到的,这可以做到但我在启动应用程序时遇到问题。 Hibernate抱怨它无法连接到我创建的h2数据库。

sed -E 's/^ *([0-9]) ([0-9a-zA-Z]*)$/\1,\2/' file

我的理论是应用程序需要启动数据库才可用,但是hibernate不会让应用程序在没有连接的情况下启动。

我是否在这条理论的正确轨道上,有没有人知道如何解决这个问题?

Hibernate配置

Caused by: org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:h2:~/todo]

h2 config

**<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
    <session-factory>
        <!--Database connection settings -->
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:h2:~/todo</property>
        <property name="connection.username">username</property>
        <property name="connection.password" />

        <!--Set the database dialect -->
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>

        <!--Echo all executed SQL to stdout-->
        <property name="show_sql">true</property>

        <!--Drop and re-create the database schema on startup-->
        <property name="hbm2ddl.auto">create</property>

        <!--Name the annotated Entity classes -->
        <mapping class="com.todo.beans.User" />

    </session-factory>
</hibernate-configuration>**

2 个答案:

答案 0 :(得分:2)

在hibernate配置中更改以下属性 <property name="connection.driver_class">org.h2.Driver</property> <property name="connection.url">jdbc:h2:mem:todo;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE </property>

  

问题在于驱动程序类;你可以保持网址原样。

答案 1 :(得分:0)

此示例项目可以为您提供帮助。

https://github.com/dornala/HotelStackoverflow

相关问题