title: Hibernate with H2 database not working in netbeans
body: <p>I can't get Hibernate to work with H2 database on netbeans with a really simple code. Please help! I get this error:</p>
  
  <pre><code>Testcase: initializationError(Teste):   Caused an ERROR
  org/hamcrest/SelfDescribing
  java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
  Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
      at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  </code></pre>
  
  <p>Here is the content of the files:</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 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;org.h2.Driver&lt;/property&gt;
      &lt;property name="hibernate.connection.url"&gt;jdbc:h2:./testeDB&lt;/property&gt;
      &lt;property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /&gt;
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>persistence.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
    &lt;persistence-unit name="HibernateAgoraVaiPU" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;class&gt;Pessoa&lt;/class&gt;
      &lt;properties&gt;
        &lt;property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/&gt;
        &lt;property name="javax.persistence.jdbc.url" value="jdbc:h2:./testeDB"/&gt;
        &lt;property name="javax.persistence.jdbc.user" value=""/&gt;
        &lt;property name="javax.persistence.jdbc.password" value=""/&gt;
        &lt;property name="dialect" value="org.hibernate.dialect.H2Dialect" /&gt;
        &lt;property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/&gt;
        &lt;property name="javax.persistence.schema-generation.database.action" value="create"/&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>Pessoa.java</strong></p>
  
  <pre><code>import java.io.Serializable;
  import java.util.Date;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.SequenceGenerator;
  import javax.persistence.Temporal;
  import javax.persistence.TemporalType;
  
  @Entity
  public class Pessoa implements Serializable {
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      private Integer id;
  
      @Column(length=50, nullable=false)
      private String nome;
  
      @Temporal(TemporalType.DATE)
      private Date aniversario;
  
      public Pessoa(){
      }
  
      public Date getAniversario() {
          return this.aniversario;
      }
  
      public void setAniversario(Date aniversario) {
          this.aniversario = aniversario;
      }
  
      public Integer getId() {
          return this.id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public String getNome() {
          return this.nome;
      }
  
      public void setNome(String nome) {
          this.nome = nome;
      }
  }
  </code></pre>
  
  <p><strong>Teste.java</strong></p>
  
  <pre><code>import java.util.Collection;
  import java.util.Date;
  import javax.persistence.EntityManager;
  import javax.persistence.Persistence;
  import org.junit.Test;
  
  
  public class Teste {
      private EntityManager em;
  
      public Teste(){
          this.em = Persistence.createEntityManagerFactory("TesteHibernatePU").createEntityManager();
      }
  
      @Test
      public void inserir(){
          // Instanciamos um objeto Pessoa, setando nome e data de nascimento
          Pessoa p1 = new Pessoa();
          p1.setNome("Jonnas Fonini");
          p1.setAniversario(new Date());
  
          Pessoa p2 = new Pessoa();
          p2.setNome("Luana Fonini");
          p2.setAniversario(new Date());
  
          Pessoa p3 = new Pessoa();
          p3.setNome("Fulano de Tal");
          p3.setAniversario(new Date());
  
          // Iniciamos a transação
          em.getTransaction().begin();
          // Aqui persistimos os objetos recém criados
          em.persist(p1);
          em.persist(p2);
          em.persist(p3);
          // E aqui efetuamos definitivamente a transação
          em.getTransaction().commit();
      }
  
      @Test
      public void listar(){
          Collection &lt;Pessoa&gt; lista = em.createQuery("from Pessoa").getResultList();
  
          for (Pessoa p : lista){
              System.out.println(p.getId() + " – " + p.getNome() + " – " + p.getAniversario());
          }
      }
  }
  </code></pre>
  
  <p>Here are the steps I made:</p>
  
  <p>1) Created a new project in NetBeans (version 8.1)</p>
  
  <p>2) Added the Hibernate Library to the project: right click over libraries > Add Library ... > Import > Hibernate 4.3.x</p>
  
  <p>3) Downloaded the latest stable version of H2 from this link
  <a href="http://repo2.maven.org/maven2/com/h2database/h2/1.3.176/h2-1.3.176.jar" rel="nofollow">http://repo2.maven.org/maven2/com/h2database/h2/1.3.176/h2-1.3.176.jar</a> and added the jar as a library: right click over libraries > Add JAR/Folder > Selected the downloaded JAR file</p>
  
  <p>4) Created a new Database connection in the Services tab: right click on databases > New connection ... > Selected new driver option > Picked the JAR file downloaded on step (3) > OK and Next > Left Username and password blank, just informed "jdbc:h2:./testeDB" (with no quotes) as the JDBC URL, and tested connection sucessfully > Select PUBLIC SCHEMA and Finished</p>
  
  <p>5) Created a new Hibernate Configuration using the assistant: right click over "source-code packages" > New > Other > Hibernate > Assitant > Next, Next, selected the connection created on step (4)</p>
  
  <p>6) Created a new Persistence Unit: right click over "source-code packages" > New > Other > Persistence > Persistence Unit > Next > Changed persistance library to Hibernate JPA 2.1, Database connection to the connection created on step (4), and table Strategy to Create</p>
  
  <p>After that I edited both persistence.xml and hibernate.cfg.xml to use org.hibernate.dialect.H2Dialect</p>
  
  <p>And finally I created a simple Person class and some test code. Also granted full permission on the project folder with </p>
  
  <pre><code>sudo chmod 777 -R HibernateAgoraVai
  </code></pre>
  
  <p>as I though might be a permission issue for creating the database file..</p>