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")
In this Hibernate H2 database tutorial, you will learn how to create a Hibernate Application to connect the H2 in-memory database. Hibernate is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.
Accessing the H2 Console H2 database has an embedded GUI console for browsing the contents of a database and running SQL queries. By default, the H2 console is not enabled in Spring. Then, after starting the application, we can navigate to http://localhost:8080/h2-console, which will present us with a login page.
H2 is an embedded, open-source, and in-memory database. It is a relational database management system written in Java. It is a client/server application. It stores data in memory, not persist the data on disk.
If we want to persist the data in the H2 database, we should store data in a file. To achieve the same, we need to change the datasource URL property. In the above property, the sampledata is a file name.
In this Hibernate H2 database tutorial, you will learn how to create a Hibernate Application to connect the H2 in-memory database. Hibernate is an object-relational mapping framework for the Java language.
jdbc:h2:mem:<database_name>; creates an in-memory database with a given database name. We can optionally initialize the In Memory H2 Database on application startup. We can pass scripts using the INIT=RUNSCRIPT FROM '<path>' in the connection string. The database scripts are located on the classpath in the src/test/resources folder.
H2 is an in memory database. Its not a persisted database. H2 is a great tool for learning because you need zero setup. Usually, the table’s are created but the URL used in H2 GUI Console is wrong. In the browser, change the database URL to jdbc:h2:mem:testdb (Shown in the screen below). You should be good to go!
The application.properties file in src/test/resources folder should contain the standard key-value pairs necessary for configuring a in memory connection. First add the dependencies for your database driver (mysql in the example below) and make the dependency for h2 test scoped.
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>" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With