title: Can't get JPA2 running with Hibernate and Maven
body: <p>Have been trying the whole day long and googled the **** out of the web ... in vain. You are my last hope:</p>
  
  <p>Here's my code:</p>
  
  <p>The Entity:</p>
  
  <pre><code>package sas.test.model;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  
  @Entity
  public class Employee {
      @Id private int id;
      private String name;
      private long salary;
      public Employee() {}
      public Employee(int id) { this.id = id; }
      public int getId() { return id; }
      public void setId(int id) { this.id = id; }
      public String getName() { return name; }
      public void setName(String name) { this.name = name; }
      public long getSalary() { return salary; }
      public void setSalary (long salary) { this.salary = salary; }
  }
  </code></pre>
  
  <p>The service class:</p>
  
  <pre><code>package sas.test.dao;
  
  import sas.test.model.Employee;
  
  import javax.persistence.*;
  import java.util.List;
  
  public class EmployeeService {
  
      protected EntityManager em;
  
      public EmployeeService(EntityManager em) {
          this.em = em;
      }
      public Employee createEmployee(int id, String name, long salary) {
          Employee emp = new Employee(id);
  
        emp.setName(name);
    emp.setSalary(salary);
    em.persist(emp);
    return emp;
  }
    public void removeEmployee(int id) {
        Employee emp = findEmployee(id);
        if (emp != null) {
            em.remove(emp);
        }
    }
    public Employee raiseEmployeeSalary(int id, long raise) {
        Employee emp = em.find(Employee.class, id);
        if (emp != null) {
            emp.setSalary(emp.getSalary() + raise);
        }
        return emp;
    }
    public Employee findEmployee(int id) {
        return em.find(Employee.class, id);
    }
  
  }
  </code></pre>
  
  <p>And the main class:</p>
  
  <pre><code>package sas.test.main;
  
  import javax.persistence.*;
  import java.util.List;
  import sas.test.model.Employee;
  import sas.test.dao.EmployeeService;
  
  
  public class ExecuteMe {
      public static void main(String[] args) {
          EntityManagerFactory emf =
                  Persistence.createEntityManagerFactory("EmployeeService");
          EntityManager em = emf.createEntityManager();
          EmployeeService service = new EmployeeService(em);
  
            // create and persist an employee
            em.getTransaction().begin();
            Employee emp = service.createEmployee(158, "John Doe", 45000);
            em.getTransaction().commit();
            System.out.println("Persisted " + emp);
            // find a specific employee
            emp = service.findEmployee(158);
            System.out.println("Found " + emp);
            // find all employees
       //     List&lt;Employee&gt; emps = service.findAllEmployees();
           // for (Employee e : emps)
           //      System.out.println("Found employee: " + e);
            // update the employee
            em.getTransaction().begin();
            emp = service.raiseEmployeeSalary(158, 1000);
            em.getTransaction().commit();
            System.out.println("Updated " + emp);
            // remove an employee
            em.getTransaction().begin();
            service.removeEmployee(158);
            em.getTransaction().commit();
            System.out.println("Removed Employee 158");
            // close the EM and EMF when done
            em.close();
            emf.close();
      }
  }
  </code></pre>
  
  <p>Finally my confs.</p>
  
  <p>pom.xml:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;Test_JPA_CRUD&lt;/groupId&gt;
      &lt;artifactId&gt;Test_JPA_CRUD&lt;/artifactId&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
      &lt;version&gt;1.0&lt;/version&gt;
      &lt;name&gt;Test_JPA_CRUD&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
      &lt;repositories&gt;
  
  
          &lt;repository&gt;
              &lt;id&gt;maven2-repository.dev.java.net&lt;/id&gt;
              &lt;name&gt;Java.net Repository for Maven&lt;/name&gt;
              &lt;url&gt;http://download.java.net/maven/2/
              &lt;/url&gt;
              &lt;layout&gt;default&lt;/layout&gt;
          &lt;/repository&gt;
  
  
          &lt;repository&gt;
              &lt;id&gt;maven.org&lt;/id&gt;
              &lt;name&gt;maven.org Repository&lt;/name&gt;
              &lt;url&gt;http://repo1.maven.org/maven2&lt;/url&gt;
              &lt;releases&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/releases&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/snapshots&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.8.1&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!--
  
                  &lt;dependency&gt;
                      &lt;groupId&gt;javax&lt;/groupId&gt;
                      &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
                      &lt;version&gt;6.0&lt;/version&gt;
                  &lt;/dependency&gt;
          --&gt;
          &lt;!--
          &lt;dependency&gt;
              &lt;groupId&gt;javax.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;persistence-api&lt;/artifactId&gt;
              &lt;version&gt;1.0&lt;/version&gt;
          &lt;/dependency&gt;
                                        --&gt;
          &lt;!-- JPA2 provider --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;3.4.0.GA&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- JDBC driver --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.derby&lt;/groupId&gt;
              &lt;artifactId&gt;derby&lt;/artifactId&gt;
              &lt;version&gt;10.5.3.0_1&lt;/version&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;3.3.2.GA&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
            &lt;artifactId&gt;ejb3-persistence&lt;/artifactId&gt;
            &lt;version&gt;3.3.2.Beta1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-annotations&lt;/artifactId&gt;
              &lt;version&gt;3.4.0.GA&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
              &lt;version&gt;1.5.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j&lt;/artifactId&gt;
              &lt;version&gt;1.2.14&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;!-- compile with mvn assembly:assembly --&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.2&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;!-- compile with mvn assembly:assembly --&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.2-beta-2&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;descriptorRefs&gt;
                          &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                      &lt;/descriptorRefs&gt;
                      &lt;archive&gt;
                          &lt;manifest&gt;
                              &lt;mainClass&gt;sas.test.main.ExecuteMe&lt;/mainClass&gt;
                          &lt;/manifest&gt;
                      &lt;/archive&gt;
                  &lt;/configuration&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;package&lt;/phase&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;!-- Force UTF-8 &amp; Java-Version 1.6 --&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.6&lt;/source&gt;
                      &lt;target&gt;1.6&lt;/target&gt;
                      &lt;!--&lt;encoding&gt;utf-8&lt;/encoding&gt;--&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>and the persistence.xml, which, I promise, is in the classpath of the target:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="1.0"
               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_1_0.xsd
  http://java.sun.com/xml/ns/persistence "&gt;
      &lt;persistence-unit name="EmployeeService" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;class&gt;sas.test.model.Employee&lt;/class&gt;
          &lt;properties&gt;
  
              &lt;property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/&gt;
              &lt;property name="hibernate.show_sql" value="true"/&gt;
              &lt;property name="javax.persistence.jdbc.url" value="jdbc:derby:webdb;create=true"/&gt;
  
  
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>As you may have noticed from some commented code, I tried both, the Hibernate and the J2EE 6 implementation of JPA2.0, however, both failed. The above-mentioned code ends up with following error:</p>
  
  <pre><code>log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
  log4j:WARN Please initialize the log4j system properly.
  Exception in thread "main" java.lang.UnsupportedOperationException: The user must supply a JDBC connection
   at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:54)
   at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
   at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
   at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
  </code></pre>
  
  <p>Any idea what's going wrong? Any "Hello World" maven/JPA2 demo that actually runs? I couldn't get any of those provided by google's search running.</p>
  
  <p>Thanx in advance.</p>