4

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Lin
  • 2,445
  • 5
  • 27
  • 37

1 Answers1

7

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • thanks. I might be wrong but from what I know schema update uses the ORMs to init the database. Is there a way to do it via an SQL script? – Lin Dec 22 '09 at 13:11
  • well, the ORM generates the SQL script and executes it, based on your mappings. It is the ideal way of doing it. I, for example, haven't had any problems with this, and didn't need any customizations or custom scripts. – Bozho Dec 22 '09 at 14:01
  • I have to use a specific script to create the schema so I cannot use the schemaUpdate option. A solution that was suggested to me is to extend the DataSource implementation and init the db there. – Lin Dec 23 '09 at 20:15
  • hm, why do you have to use a specific script? – Bozho Dec 23 '09 at 20:27
  • 2
    I had a few things that I didn't want hibernate to do, so I wanted a specific script. What I finally used (when I was able not to use the script) is - create thanks for the help – Lin May 04 '10 at 11:18