title: javax persistence rollbackException
body: <p>I'm new to JPA and I'm trying some basic relationships.</p>
  
  <p>I have two entities</p>
  
  <pre><code> @Entity
  @Table(name = "relationshipDepartment")
  public class Department implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue()
      private Long id;
      @Version
      @Column(name = "OPTLOCK")
      private long version;
      private String name;
      private String code;
      @OneToOne
      private Employee manager;
  
      public Department() {
          super();
      }
  
       getters and setters
  </code></pre>
  
  <p>and </p>
  
  <pre><code>    @Entity
  @Table(name = "relationshipEmployee")
  public class Employee implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue()
      private Long id;
      @Version
      @Column(name = "OPTLOCK")
      private long version;
      private String name;
      private String title;
      private double salary;
      @ManyToOne
      private Employee supervisor;
      // A employee is a member of one department
      @ManyToOne
      private Department department;
  
      public long getVersion() {
          return version;
      }
      getters and setters
  </code></pre>
  
  <p>When I just try to add some entities like this ;</p>
  
  <pre><code>   public class Starter {
  
      /**
       * @param args
       */
      public static void main(String[] args) {
  
          Employee ceo  = new Employee ();
          Employee manager1 = new Employee ();
          Employee manager2 = new Employee ();
  
          ceo.setName("Bill Clinton");
          ceo.setTitle("CEO");
          ceo.setSalary(3800.0);
          ceo.setSupervisor(ceo);
  
          manager1.setName("Hilary Clinton");
          manager1.setTitle("Manager");
          manager1.setSalary(3200.0);
          manager1.setSupervisor(ceo);
  
          manager2.setName("Tim Reyback");
          manager2.setTitle("Manager");
          manager2.setSalary(3200.0);
          manager2.setSupervisor(ceo);
  
          Department finance = new Department ();
          Department research = new Department ();
  
          finance.setCode("FIN");
          finance.setName("Finance");
  
          research.setCode("RES");
          research.setName("Research");
  
          ceo.setDepartment(finance);
          manager1.setDepartment(finance);
          manager2.setDepartment(research);
  
          finance.setManager(manager1);
          research.setManager(manager2);
  
          addEmployee(manager1);
          addEmployee(manager2);
  
          addDepartement(finance);
          addDepartement(research);
  
          System.out.println("All the employees");
  
          List&lt;Employee&gt; employees = retrieveEmployees();
          for (Employee aEmployee : employees) {
              System.out.println(aEmployee.toString());
          }       
  
          System.out.println("All the departments");
  
          List&lt;Department&gt; departments = retrieveDepartments();
          for (Department aDepartment : departments) {
              System.out.println(aDepartment.toString());
          }   
      }
  
  
      private static void addEmployee(Employee employee) {
          EntityManagerFactory emf = Persistence
                  .createEntityManagerFactory("JPAex4");
          EntityManager em = emf.createEntityManager();
          try {
              em.getTransaction().begin();
              em.persist(employee);
              em.getTransaction().commit();
          } catch (Exception e) {
              logging.error("erorr at adding a employee"
                      + " :" + e);
          } finally {
              // Close all the connections:
              em.close();
              emf.close();
          }
      }
  
  
      private static List&lt;Employee&gt; retrieveEmployees() {
          EntityManagerFactory emf = Persistence
                  .createEntityManagerFactory("JPAex4");
          EntityManager em = emf.createEntityManager();
          List&lt;Employee&gt; results = null;
          try {
              // Retrieve all the Employee objects from the database:
              TypedQuery&lt;Employee&gt; query = em.createQuery(
                      "SELECT e FROM Employee e", Employee.class);
              // Creation of the Userlist
              results = query.getResultList();
              return results;
          } catch (Exception e) {
              logging.error("error at the employeelist"
                      + " :" + e);
          } finally {
              // Close all the connections:
              em.close();
              emf.close();
          }
          return results;
      }
  
      private static void addDepartement(Department department) {
              EntityManagerFactory emf = Persistence
                  .createEntityManagerFactory("JPAex4");
          EntityManager em = emf.createEntityManager();
          try {
              em.getTransaction().begin();
              em.persist(department);
              em.getTransaction().commit();
          } catch (Exception e) {
              logging.error("error department"
                      + " :" + e);
          } finally {
              // Close all the connections:
              em.close();
              emf.close();
          }
      }
  
  
      private static List&lt;Department&gt; retrieveDepartments() {
  
          EntityManagerFactory emf = Persistence
                  .createEntityManagerFactory("JPAex4");
          EntityManager em = emf.createEntityManager();
          List&lt;Department&gt; results = null;
          try {
              // Retrieve all the Employee objects from the database:
              TypedQuery&lt;Department&gt; query = em.createQuery(
                      "SELECT e FROM Department e", Department.class);
              // Creation of the Userlist
              results = query.getResultList();
              return results;
          } catch (Exception e) {
              logging.error("Error departementlist"
                      + " :" + e);
          } finally {
              // Close all the connections:
              em.close();
              emf.close();
          }
          return results;
      }
  
  }
  </code></pre>
  
  <p>I get a : </p>
  
  <p>:javax.persistence.RollbackException: Error while committing the transaction</p>
  
  <pre><code>at JPA.starter.Starter.main(Starter.java:52) -&gt; first adding line
  </code></pre>
  
  <p>I don't see anything wrong.  Any suggestions?</p>
  
  <p>Thx all</p>