1

In my project I use h2 in memory database, and I want it to be created not by Hibernate, but with by a SQL script. Here is my hibernate.properties

I made

hibernate.hbm2ddl.auto=none

none to disable autocreation of database, and added

hibernate.hbm2ddl.import_files=schema.sql,insert-users.sql

schema.sql contains SQL code to create schema, and then to insert-users.sql and it contains the initial data.

The project builds successfully, but when I try to hit database, I get a Table <tablename> not found exception.

4
  • hibernate.dialect=org.hibernate.dialect.H2Dialect hibernate.connection.driver.class=org.h2.Driver hibernate.show_sql=true hibernate.hbm2ddl.auto=none hibernate.connection.url=jdbc:h2:memhelpdeskdb hibernate.hbm2ddl.import_files=schema.sql,insert-users.sql Commented Dec 6, 2017 at 16:58
  • Here are my properties, I don't know why, but I cant add it to the post... Commented Dec 6, 2017 at 16:58
  • Corrected mispelling in title. Highlighted some pertinent words. Changed error message to display as code and thus display the full message (<tablename> was missing as <> are used to indicate quoted text). Commented Dec 7, 2017 at 6:36
  • How are you getting on with the answers below, Fairy? One was posted on the same day, and one was posted the day after your question. Commented Dec 31, 2017 at 16:22

2 Answers 2

3

Since Hibernate won't do this for you unless you use create or create-drop hbm2ddl, there are other ways to achieve what you want.

Specialized tools

There are tools that are created specifically for this: Flyway, LiquiBase. These are often configured to be run when the app is deployed and allow you to version DB scheme. They are applicable not only for testing (and mainly - not for testing), but for production as well. They can ensure that the scheme on all your envs is the same. If you use these tools, then it's better to set hbm2ddl to validate.

Spring's support

Less widespread way is to use Spring's support for embedded DBs:

<jdbc:embedded-database id="dataSource">
    <jdbc:script location="classpath:schema.sql"/>
    <jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>

Data for testing

If the intention is to create data for testing (not scheme), then it's better to create entities and use your DAO/Repository layer to persist those in tests. This way you don't duplicate mechanisms of persisting data.

Sign up to request clarification or add additional context in comments.

Comments

0

Two comments from the Hibernate documentation are relevant here:

This is useful for testing or demoing: by adding INSERT statements for example you can populate your database with a minimal set of data when it is deployed.

and

These statements are only executed if the schema is created ie if hibernate.hbm2ddl.auto is set to create or create-drop.

I'm not too sure that the import functionality will do what you want it to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.