What you ask is called Nested Transactions. JPA alone does not specify anything about that. A Persistence Unit can be of two types: RESOURCE_LOCAL (your case, as you work with EntityTransaction
) and JTA. Also JTA is known as not requiring nested transactions. But your case is RESOURCE_LOCAL, which AFIK depends on your JDBC driver, database and database engine.
IMPORTANT NOTE: the example that you showed is an example of parallel transactions, as every EntityManager
instance you create with EntityManagerFactory.createEntityManager()' is independent of each other. In your case, if
createApplication()fails,
createTerminal()` will have its transaction commited (please do test it and comfirm that in a comment)!
Usually you come along without nested transaction and I would suggest using JTA transaction (with a framework that supports declarative transactions, like Spring or EJB), as it is really a standard nowadays.