【问题标题】:javax persistence rollbackExceptionjavax持久性回滚异常
【发布时间】:2012-12-25 15:20:35
【问题描述】:

我是 JPA 的新手,我正在尝试一些基本的关系。

我有两个实体

 @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

    @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

当我尝试添加一些这样的实体时;

   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<Employee> employees = retrieveEmployees();
        for (Employee aEmployee : employees) {
            System.out.println(aEmployee.toString());
        }       

        System.out.println("All the departments");

        List<Department> 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<Employee> retrieveEmployees() {
        EntityManagerFactory emf = Persistence
                .createEntityManagerFactory("JPAex4");
        EntityManager em = emf.createEntityManager();
        List<Employee> results = null;
        try {
            // Retrieve all the Employee objects from the database:
            TypedQuery<Employee> 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<Department> retrieveDepartments() {

        EntityManagerFactory emf = Persistence
                .createEntityManagerFactory("JPAex4");
        EntityManager em = emf.createEntityManager();
        List<Department> results = null;
        try {
            // Retrieve all the Employee objects from the database:
            TypedQuery<Department> 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;
    }

}

我得到一个:

:javax.persistence.RollbackException: 提交事务时出错

at JPA.starter.Starter.main(Starter.java:52) -> first adding line

我看不出有什么问题。有什么建议吗?

谢谢大家

【问题讨论】:

  • 向我们展示异常的完整堆栈跟踪,从其确切的错误消息开始。并重新阅读您的 JPA 书。 EntityManagerFactory 应该只创建一次。这是一个重量级的线程安全对象。
  • 只是为了这个简单的练习。我只是不明白为什么要回滚。
  • 是的。所以,既然是锻炼,那就做坏事吧。如果您希望我们帮助您了解回滚的原因,为什么不向我们提供堆栈跟踪信息?
  • 首先,我的老师是这样教我的。如果您建议另一种(更好的)方式,那么建议可能会比咬我的鼻子来回答更好。其次,我现在不在我的家用电脑上,所以我无法发布完整的堆栈跟踪,但它本身的问题很清楚。

标签: java jpa


【解决方案1】:

一个问题是,即使对于已经被持久化的对象,你也会调用 persists。当您持久化 manager1(通过 addEmployee())时,JPA 也将持久化财务。因此,您不能在 addDepartment() 中再次保留它。我建议只保留一个函数用于持久化对象,并在传递根对象后调用此函数。

【讨论】:

    猜你喜欢
    • 2011-09-15
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    相关资源
    最近更新 更多