环境: Java EE7,JPA(EclipseLink),JTA,Glassfish 4.1.1
我想回滚多个实体创建。
我的代码基本上如下:
TestBean.java
@Named
@RequestScoped
public class TestBean implements Serializable {
@Inject
private TestService service;
private String value1, value2, value3;
public String saveThreeEntities(){
service.createThreeEntities(value1, value2, value3);
}
}
TestService.java
@ApplicationScoped
public class TestService {
@PersistenceContext
EntityManager em;
@Transactional
public void createThreeEntities(String value1, String value2, String value3) {
TestEntity entity1 = new TestEntity();
entity1.setValue(value1);
TestEntity entity2 = new TestEntity();
entity2.setValue(value2);
TestEntity entity3 = new TestEntity();
entity3.setValue(value3);
em.persist(entity1);
em.persist(entity2);
em.persist(entity3);
}
}
我希望JPA执行回滚所有三个实体,因为例如由于违反名称唯一性而无法仅保留entity3。 但不幸的是,JPA持续存在entity1和entity2并且不会持续存在实体3。
我尝试了很多方法,但是我的代码不能像我期望的那样工作。
How to rollback a series of persist statements in JPA?
How to rollback transaction in JPA?
How to rollback a series of persist statements in JPA?
我需要帮助。