Environment: Java EE7, JPA(EclipseLink), JTA, Glassfish 4.1.1
I want to rollback multiple entities creation.
My codes are as follows essentially:
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);
}
}
I hope JPA performs rollback all three entities when it fails to persist only entity3 due to the violation of name uniqueness for example. But JPA persists entity1 and entity2 and dose not persist entity3 unfortunately.
I have tried many approaches, but my codes dose not work as I expect.
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?
I need help.