Utiliser H2 en mémoire avec Hibernate

Dépendances dans Maven

pom.xml

    <!-- Pour Hibernate JPA2 (voir doc. de Hibernate) -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.....</version>
    </dependency>

    <!-- Pour H2 -->
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>......</version>
    </dependency>

Configuration de Hibernate et JDBC pour utiliser H2 en mémoire

A CHAQUE ANNÉE VOUS VOUS FAITES PRENDRE MALGRÉ LE WARNING!! LISEZ BIEN LES ATTRIBUTS DE LA BALISE <PERSISTENCE-UNIT>

src/main/resources/META-INF/persistence.xml

<?xml version="1.0"  encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/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">

  <persistence-unit name="monapplication" transaction-type="RESOURCE_LOCAL">
    <description>H2 In Memory DB</description>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

    <exclude-unlisted-classes>false</exclude-unlisted-classes>


    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
      <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
      <property name="hibernate.hbm2ddl.auto" value="create-drop" />
      <property name="hibernate.show_sql" value="true" /> <!-- DEBUG -->
      <property name="hibernate.format_sql" value="true" /> <!-- DEBUG -->

      <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="hibernate.archive.autodetection" value="class, hbm" />
      <property name="hibernate.id.new_generator_mappings" value="true" />

    </properties>
  </persistence-unit>
</persistence>