17

I'm working with Hibernate. How can I configure my persistence.xml to have an H2 in-memory database?

My persistence.xml is:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="persistenceUnit"
        transaction-type="RESOURCE_LOCAL">

        <class>com.mastertheboss.domain.Employee</class>
        <class>com.mastertheboss.domain.Department</class>
        <properties>

            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="hbm2ddl.auto" value="update" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
        </properties>

    </persistence-unit>
</persistence>

But when I run my app I get the following error:

Internal Exception: org.h2.jdbc.JdbcSQLException: Table "EMPLOYEE" not found; SQL statement: SELECT ID, NAME, DEPARTMENT_ID FROM EMPLOYEE [42102-171] Error Code: 42102 Call: SELECT ID, NAME, DEPARTMENT_ID FROM EMPLOYEE Query: ReadAllQuery(referenceClass=Employee sql="SELECT ID, NAME, DEPARTMENT_ID FROM EMPLOYEE")

1 Answer 1

27

You should set hibernate.hbm2ddl.auto property to "create" the first time you run your application, to create the tables

<property name="hibernate.hbm2ddl.auto" value="create" />

and then (if you don't want the tables to be recreated and emptied every time you start) set it to "validate".

<property name="hibernate.hbm2ddl.auto" value="validate" />

To create the schema automatically, add if-not-exists to your connection url like this:

<property name="hibernate.connection.url" value="jdbc:h2:~/<filename>;INIT=CREATE SCHEMA IF NOT EXISTS <schema_name>" />
Sign up to request clarification or add additional context in comments.

7 Comments

@andPat this soulution is probably the most correct. If you can't get this to work, the problem is probably somewhere else.
but if I do so I have to delete ` <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />`, am I right?
Yes, you should replace that one.
Do not forget to fill in your <filename> and <schema_name>.
Do you have any clue as to why create will add the constraints, but update will not? Even when I start with an empty (nonexisting even), or in-memory database?
|

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.