title: Unable to connect H2 DB with Hibernate
body: <p>I am trying to create a small H2 in-memory database while using hibernate. But the table is not dispplayed in the H2 UI. I'm not sure if I need to include anything else in the configuration.</p>
  
  <p>Here is the code:
  Below are dependencies added for hibernate and h2 DB.</p>
  
  <p><strong>pom.xml</strong> </p>
  
  <pre><code>&lt;dependency&gt;  
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;  
      &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;  
      &lt;version&gt;5.3.1.Final&lt;/version&gt;  
  &lt;/dependency&gt; 
  
  &lt;dependency&gt;  
      &lt;groupId&gt;com.h2database&lt;/groupId&gt;  
      &lt;artifactId&gt;h2&lt;/artifactId&gt;  
      &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;version&gt;1.4.200&lt;/version&gt; 
  
  &lt;/dependency&gt;  
  </code></pre>
  
  <p>This is the configuration XML which has all the H2 DB details.</p>
  
  <p><strong>hibernate.cfg.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
  &lt;!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
          "http://www.hibernate.org/dtd/hibernate-configuration-5.3.dtd"&gt;  
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="connection.driver_class"&gt;org.h2.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:h2:mem:test;DB_CLOSE_DELAY=-1&lt;/property&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
          &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;
          &lt;mapping class="HiberDemo.Employee"/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>This creates the table along with the other columns.</p>
  
  <p><strong>Employee.java</strong></p>
  
  <pre><code>package HiberDemo;
  
  import javax.persistence.Entity;  
  import javax.persistence.Id;  
  import javax.persistence.Table;  
  
  @Entity  
  @Table(name= "emp500")   
  public class Employee {    
  
  @Id   
  private int id;    
  private String firstName,lastName;    
  
  public int getId() {    
      return id;    
  }    
  public void setId(int id) {    
      this.id = id;    
  }    
  public String getFirstName() {    
      return firstName;    
  }    
  public void setFirstName(String firstName) {    
      this.firstName = firstName;    
  }    
  public String getLastName() {    
      return lastName;    
  }    
  public void setLastName(String lastName) {    
      this.lastName = lastName;    
  }    
  }
  </code></pre>
  
  <p>This is to store some data into the table.</p>
  
  <p><strong>StoreData.java</strong></p>
  
  <pre><code>package HiberDemo;
  
  import java.sql.DriverManager;
  import java.sql.SQLException;
  
  import org.hibernate.Session;    
  import org.hibernate.SessionFactory;    
  import org.hibernate.Transaction;  
  import org.hibernate.boot.Metadata;  
  import org.hibernate.boot.MetadataSources;  
  import org.hibernate.boot.registry.StandardServiceRegistry;  
  import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  
  import com.sun.corba.se.pept.transport.Connection;  
  
  
  public class StoreData {    
  public static void main(String[] args) throws SQLException {    
  
  
      StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
      Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();  
  
  SessionFactory factory = meta.getSessionFactoryBuilder().build();  
  Session session = factory.openSession();  
  Transaction t = session.beginTransaction();   
  
  
  
      Employee e1=new Employee();    
      e1.setId(101);    
      e1.setFirstName("abc");    
      e1.setLastName("xyz");    
  
      session.save(e1);  
      t.commit();  
      System.out.println("successfully saved");    
      factory.close();  
      session.close();   
     // conn.close();
  
  }    
  } 
  </code></pre>
  
  <p>This is the H2 DB UI where the table I have created is not displayed.</p>
  
  <p><a href="https://i.stack.imgur.com/yA1C2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yA1C2.png" alt="H2 DB UI"></a></p>