[英]Running H2 embedded database in spring / hibernate
I am trying to create an spring boot application utilizing hibernate and h2. 我正在尝试使用hibernate和h2创建一个spring boot应用程序。 From what I have found online this can be done but I am having a problem starting the application. 根据我在网上找到的信息,可以完成此操作,但是启动应用程序时遇到问题。 Hibernate is complaining that it cannot make a connection to the h2 database I have created. Hibernate抱怨它无法与我创建的h2数据库建立连接。
Caused by: org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:h2:~/todo]
My theory is that the application needs to start for the database be available but hibernate is not letting the application start without the connection. 我的理论是,应用程序需要启动才能使数据库可用,但是休眠状态不允许应用程序在没有连接的情况下启动。
Am I on the right track with this theory, has there been similar issues that someone knows how to get around this? 我是否掌握了这一理论,是否有类似的问题有人知道如何解决?
Hibernate 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>**
h2 config h2配置
import org.h2.server.web.WebServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebConfiguration {
@Bean
ServletRegistrationBean h2servletRegistration(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
registrationBean.addUrlMappings("/console/*");
return registrationBean;
}
}
change following properties in hibernate config <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>
更改休眠配置中的以下属性<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>
Problem is with driver class; 问题出在司机课上。 you may keep url as it is. 您可以按原样保留网址。
This sample project can help you. 该示例项目可以为您提供帮助。
https://github.com/dornala/HotelStackoverflow https://github.com/dornala/HotelStackoverflow
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.