我正在从一些在线教程中学习Eclipselink。目前在持久存在对象时遇到问题。
线程中的异常" main" java.lang.IllegalStateException:异常 描述:目前没有任何交易处于活动状态 org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:123) 在 com.training.service.EmployeePersistance.main(EmployeePersistance.java:23)
使用JPA连接器的数据库连接工作正常。我已经通过执行
测试了连接System.out.println(Persistence.createEntityManagerFactory("_EclipseLinkTraining").isOpen());
返回true。表未在DB中创建。执行代码后,表将自动生成,但对象不会持久化。以下是导致问题的代码: 实体类:
@Entity
public class Employee {
@Id
int empId;
@Basic(optional=true)
String empName;
//getters and setters
}
persistance.xml:
<persistence-unit name="_EclipseLinkTraining" transaction-type="RESOURCE_LOCAL">
<class>com.training.entity.Employee</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/ecpliselink_training"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
java main方法:
public static void main(String[] args) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("_EclipseLinkTraining");
EntityManager manager = factory.createEntityManager();
manager.getTransaction();
Employee emp = new Employee();
emp.setEmpId(1);
emp.setEmpName("sam");
manager.persist(emp);
manager.getTransaction().commit();
manager.close();
factory.close();
System.out.println("completed");
}