简体   繁体   English

H2数据库在内存中 - 通过Spring / Hibernate的Init模式

[英]H2 database In memory - Init schema via Spring/Hibernate

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

1. I want spring to start the H2 database so I created the following Spring beans: 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? 我是否需要使用tcp服务器才能在内存中使用? Is this the correct configuration for in memory? 这是内存中的正确配置吗?

2.With the above configuration - How can I create and init the database schema before Hibernate is started? 2.使用上面的配置 - 如何在Hibernate启动之前创建和初始化数据库模式? I know that HSQLDB has a URL property that states the name of the creation script. 我知道HSQLDB有一个URL属性,它声明了创建脚本的名称。 Is there a similar way here? 这里有类似的方法吗?

Thanks for the help 谢谢您的帮助

Hibernate has a property called schemaUpdate . Hibernate有一个名为schemaUpdate的属性。 Set it on your SessionFactory so that the database is created on initialization. SessionFactory上设置它,以便在初始化时创建数据库。

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

If you are using JPA, then there is a generateDdl property that is to be set on the JpaVendorAdapter 如果您正在使用JPA,那么将在JpaVendorAdapter上设置generateDdl属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM