H2 database In memory - Init schema via Spring/Hibernate

社会主义新天地 提交于 2019-12-18 17:34:11

问题


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>

Do I need to use the tcp server at all for in-memory use? 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? 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


回答1:


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

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

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



来源:https://stackoverflow.com/questions/1945175/h2-database-in-memory-init-schema-via-spring-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!