H2数据库在内存中 - 通过Spring / Hibernate启动模式 [英] H2 database In memory - Init schema via Spring/Hibernate

查看:409
本文介绍了H2数据库在内存中 - 通过Spring / Hibernate启动模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个H2数据库的Spring / Hibernate应用程序,在配置H2以嵌入模式运行时(内存中),我遇到了一些问题:


1.我希望spring能够启动H2数据库,所以我创建了以下Spring bean:

I have a Spring/Hibernate application with H2 database and I have a few issues with configuring H2 to run in an embedded mode (in memory):

1. I want spring to start the H2 database so I created the following Spring beans:

<bean id="org.h2.tools.Server" class="org.h2.tools.Server"
        factory-method="createTcpServer" init-method="start" destroy-method="stop">
        <constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,8043" />
    </bean>

    <bean id="org.h2.tools.Server-WebServer" class="org.h2.tools.Server"
        factory-method="createWebServer" init-method="start">
        <constructor-arg value="-web,-webAllowOthers,true,-webPort,8082" />
    </bean>

我是否需要在内存中使用tcp服务器?这是在内存中的正确配置吗?

Do I need to use the tcp server at all for in-memory use? Is this the correct configuration for in memory?

2.有了上述配置 - 如何在Hibernate启动之前创建并初始化数据库模式?
我知道HSQLDB有一个URL属性,它指出了创建脚本的名称。这里有类似的方法吗?

2.With the above configuration - How can I create and init the database schema before Hibernate is started? I know that HSQLDB has a URL property that states the name of the creation script. Is there a similar way here?

感谢您的帮助

Thanks for the help

推荐答案

Hibernate有一个名为 schemaUpdate 的属性。将它设置在你的 SessionFactory 上,这样数据库就可以在初始化时创建。

Hibernate has a property called schemaUpdate. Set it on your SessionFactory so that the database is created on initialization.

<property name="schemaUpdate" value="true" />

如果您使用的是JPA,那么有一个 generateDdl 要在 JpaVendorAdapter

If you are using JPA, then there is a generateDdl property that is to be set on the JpaVendorAdapter

这篇关于H2数据库在内存中 - 通过Spring / Hibernate启动模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆