java 内存中的 H2 数据库 - 通过 Spring/Hibernate 初始化模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1945175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:38:46  来源:igfitidea点击:

H2 database In memory - Init schema via Spring/Hibernate

javahibernatespringormh2

提问by Lin

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:

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

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

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

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

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

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?

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

Thanks for the help

谢谢您的帮助

回答by Bozho

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

Hibernate 有一个名为schemaUpdate. 将其设置为您SessionFactory的数据库,以便在初始化时创建数据库。

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

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

如果您使用的是 JPA,则generateDdl需要在JpaVendorAdapter