目前没有任何交易活跃

时间:2013-12-04 12:15:46

标签: java jpa eclipselink desktop-application

所以我在这里有这个代码

User us = new User();
            us.setId(id1);
            us.setUsername(userNameTextField.getText());
            us.setPassowrd(passwordFormattedTextField.getText());
            us.setLevel(levelComboBox.getSelectedItem().toString());
            us.setEmpid(getCurrentEmp());
            getCurrentEmp().getUserCollection().add(us);
            em.getTransaction().begin();               
            em.persist(getCurrentEmp());
            em.flush();
            em.getTransaction().commit();

但是当我尝试提交时我得到了这个错误

Exception Description: No transaction is currently active
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.throwCheckTransactionFailedException(EntityTransactionWrapper.java:87)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.checkForTransaction(EntityTransactionWrapper.java:50)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.checkForTransaction(EntityManagerImpl.java:2015)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:846)

并且我没有得到y因为我已经调用了entitymanager实例,如下所示

em = emi.getInstance();

来自这个班级

public class EmInstance {
EntityManager em;
public EntityManager getInstance(){
em = Persistence.createEntityManagerFactory("samplePU").createEntityManager();
return em;
} 

}

我的问题是,当我想在提交,更新或任何其他交易过程中使用e.i时,如何使交易处于活动状态。我正在使用eclipselink及其桌面应用程序

1 个答案:

答案 0 :(得分:3)

您确定两次调用em.getTransaction()会让您回复相同的交易吗? 试试:

EntityTransaction etx = em.getTransaction();
etx.begin();
...
etx.commit();