title: h2 database persists data but resets on application start
body: <p>I have a basic test for an embedded database that I'm trying to get working for a java project. As it stands now I am able to save rows to the database with my entity repository object, and after the app finishes running, I can connect to the database with intellij and see those rows are still there. But then, if I comment out the <code>save</code> methods and run it again, when I check the database after it finishes, the database is empty.</p>
  <h2>Entity</h2>
  <pre class="lang-java prettyprint-override"><code>package closet.utilities.entities;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = &quot;outfits&quot;)
  public class Outfit {
  
      @Id
      @Column(name = &quot;id&quot;)
      String id;
  
      @Column(name = &quot;inv_name&quot;)
      String invName;
  
      @Column(name = &quot;display_name&quot;)
      String displayName;
  
      @Column(name = &quot;owner&quot;)
      String owner;
  
      public Outfit() {
      }
  
      public Outfit(String id, String invName, String displayName, String owner) {
          this.id = id;
          this.invName = invName;
          this.displayName = displayName;
          this.owner = owner;
      }
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
      public String getInvName() {
          return invName;
      }
  
      public void setInvName(String invName) {
          this.invName = invName;
      }
  
      public String getDisplayName() {
          return displayName;
      }
  
      public void setDisplayName(String displayName) {
          this.displayName = displayName;
      }
  
      public String getOwner() {
          return owner;
      }
  
      public void setOwner(String owner) {
          this.owner = owner;
      }
  
      @Override
      public String toString() {
          return &quot;Outfit{&quot; +
                  &quot;id='&quot; + id + '\'' +
                  &quot;, invName='&quot; + invName + '\'' +
                  &quot;, displayName='&quot; + displayName + '\'' +
                  &quot;, owner='&quot; + owner + '\'' +
                  '}';
      }
  }
  </code></pre>
  <h2>Repository</h2>
  <pre class="lang-java prettyprint-override"><code>package closet.utilities.repositories;
  
  import closet.utilities.entities.Outfit;
  
  import javax.persistence.EntityManager;
  import java.util.List;
  import java.util.Optional;
  
  public class OutfitRepository {
  
      private EntityManager entityManager;
  
      public OutfitRepository(EntityManager entityManager) {
          this.entityManager = entityManager;
      }
  
      public Optional&lt;Outfit&gt; findById(String id) {
          Outfit outfit = entityManager.find(Outfit.class, id);
          return outfit != null ? Optional.of(outfit) : Optional.empty();
      }
  
      public List&lt;Outfit&gt; findAll() {
          return entityManager.createQuery(&quot;from Outfit&quot;).getResultList();
      }
  
      public Optional&lt;Outfit&gt; save(Outfit outfit) {
          try {
              entityManager.getTransaction().begin();
              entityManager.persist(outfit);
              entityManager.getTransaction().commit();
              return Optional.of(outfit);
          } catch (Exception e) {
              // TODO logging
              e.printStackTrace();
          }
          return Optional.empty();
      }
  }
  </code></pre>
  <h2>main method</h2>
  <pre class="lang-java prettyprint-override"><code>package closet.utilities;
  
  import closet.utilities.entities.Outfit;
  import closet.utilities.repositories.OutfitRepository;
  import org.hibernate.Session;
  import org.hibernate.Transaction;
  
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.Persistence;
  import java.util.List;
  
  public class Runnable {
  
      public static void main(String[] args) {
  
          // Create our entity manager
          EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(&quot;closet&quot;);
          EntityManager entityManager = entityManagerFactory.createEntityManager();
  
  
          Outfit outfit = new Outfit(&quot;Ramesh&quot;, &quot;Fadatare&quot;, &quot;rameshfadatare@javaguides.com&quot;, &quot;&quot;);
          Outfit outfit1 = new Outfit(&quot;John&quot;, &quot;Cena&quot;, &quot;john@javaguides.com&quot;, &quot;&quot;);
          OutfitRepository outfitRepository = new OutfitRepository(entityManager);
          //outfitRepository.save(outfit);
          //outfitRepository.save(outfit1);
  
          List&lt;Outfit&gt; outfits = outfitRepository.findAll();
  
          for (Outfit o : outfits) {
              System.out.println(o.getInvName());
          }
  
      }
  }
  </code></pre>
  <h2>persistence.xml</h2>
  <pre class="lang-xml prettyprint-override"><code>&lt;persistence xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
               xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/persistence
                        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd&quot;
               version=&quot;2.0&quot; xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot;&gt;
  
      &lt;persistence-unit name=&quot;closet&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
          &lt;class&gt;closet.utilities.entities.Outfit&lt;/class&gt;
          &lt;properties&gt;
              &lt;property name=&quot;javax.persistence.jdbc.driver&quot; value=&quot;org.h2.Driver&quot; /&gt;
              &lt;property name=&quot;javax.persistence.jdbc.url&quot;    value=&quot;jdbc:h2:./data/closet&quot; /&gt;
              &lt;property name=&quot;javax.persistence.jdbc.user&quot; value=&quot;sa&quot; /&gt;
              &lt;property name=&quot;javax.persistence.jdbc.password&quot; value=&quot;&quot; /&gt;
  
              &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.H2Dialect&quot;/&gt;
              &lt;property name=&quot;hibernate.hbm2ddl.auto&quot; value=&quot;create-drop&quot; /&gt;
              &lt;property name=&quot;show_sql&quot; value=&quot;true&quot;/&gt;
              &lt;property name=&quot;hibernate.temp.use_jdbc_metadata_defaults&quot; value=&quot;false&quot;/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  <h2>hibernate.cfg.xml</h2>
  <pre class="lang-xml prettyprint-override"><code>&lt;!DOCTYPE hibernate-configuration PUBLIC
          &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;
          &quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;!-- JDBC Database connection settings --&gt;
          &lt;property name=&quot;connection.driver_class&quot;&gt;org.h2.Driver&lt;/property&gt;
          &lt;property name=&quot;connection.url&quot;&gt;jdbc:h2:./data/closet&lt;/property&gt;
          &lt;property name=&quot;connection.username&quot;&gt;sa&lt;/property&gt;
          &lt;property name=&quot;connection.password&quot;&gt;&lt;/property&gt;
          &lt;!-- JDBC connection pool settings ... using built-in test pool --&gt;
          &lt;property name=&quot;connection.pool_size&quot;&gt;1&lt;/property&gt;
          &lt;!-- Select our SQL dialect --&gt;
          &lt;property name=&quot;dialect&quot;&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
          &lt;!-- Echo the SQL to stdout --&gt;
          &lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;
          &lt;!-- Set the current session context --&gt;
          &lt;property name=&quot;current_session_context_class&quot;&gt;thread&lt;/property&gt;
          &lt;!-- Drop and re-create the database schema on startup --&gt;
          &lt;property name=&quot;hbm2ddl.auto&quot;&gt;create-drop&lt;/property&gt;
          &lt;!-- dbcp connection pool configuration --&gt;
          &lt;property name=&quot;hibernate.dbcp.initialSize&quot;&gt;5&lt;/property&gt;
          &lt;property name=&quot;hibernate.dbcp.maxTotal&quot;&gt;20&lt;/property&gt;
          &lt;property name=&quot;hibernate.dbcp.maxIdle&quot;&gt;10&lt;/property&gt;
          &lt;property name=&quot;hibernate.dbcp.minIdle&quot;&gt;5&lt;/property&gt;
          &lt;property name=&quot;hibernate.dbcp.maxWaitMillis&quot;&gt;-1&lt;/property&gt;
          &lt;mapping class=&quot;closet.utilities.entities.Outfit&quot; /&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  <p>I'm just following <a href="https://www.infoworld.com/article/3373652/java-persistence-with-jpa-and-hibernate-part-1-entities-and-relationships.html" rel="nofollow noreferrer">this tutorial</a> here as a guide to just figure out how to get it all working. I can't see what I'm doing wrong based on that.</p>