title: Problem in Handling Unit of work using Hibernate JPA
body: <p>I use Spring +  Hibernate + JPA</p>
  
  <p>I  need to handle the list of customers by inserting their orders.</p>
  
  <p>Here is the Unit of work :</p>
  
  <pre><code>for(Customer customer: CustomerList) {
      List&lt;Order&gt; orderList =  customer.getOrders();
      for(Order order: OrderList) {
         //1. Insert order into ORDER table
              //If insert fails due to Duplicate key then no rollback and I follow steps 2 &amp; 3. 
              //If insert fails due to any reason except duplicate key then rollback all the previous transactions
         //2. select the order record (If any issue during selection then rollbackall the previous transactions)
         //3. Update the order If status of the order is different from that in DB (If any issue during update then rollback all the previous transactions)
      }
      // 4. Update Customer record into CUSTOMER table (If any issue during update then rollback all the previous transactions)
  }
  </code></pre>
  
  <p>Commit is required when all the orders and customer db processes are ok.</p>
  
  <ol>
  <li><p>Insert order </p>
  
  <p>1.a If duplicate order do not roll back. But select that order from table and update if the status of the order is different in the req compared to the one in db</p>
  
  <p>1.b If any other error during inserting of ORDER then roll back</p>
  
  <p>1.c If no error then proceed inserting orders of the particular Customer</p></li>
  <li><p>Once orders of the particular Customer is done, then update Customer table</p></li>
  <li><p>Loop continues..</p></li>
  <li><p>While handling point 1, 2 and 3 If everything is ok, then commit is required. 
  If any issues in the middle then all transactions are rolled back</p></li>
  </ol>
  
  <p>Controller --> Facade layer --> Service --> Repository/Dao </p>
  
  <p>Facade:</p>
  
  <pre><code>@Autowired
  MyServiceBean serviceBean;
  
  @Transactional(noRollbackFor = {EntityExistsException.class, PersistException.class, ConstraintViolationException.class, DataIntegrityViolationException.class})
  @override
  public void facadeMethod(MyReq req) { 
       List&lt;Customer&gt; custList = req.getCustomers():
       for(Customer customer: CustList) {
          List&lt;Order&gt; orderList = customer.getOrders();
          for(Order order: orderList) {
            String dbAction = "";
             try {
                 dbAction = serviceBean.insertOrder(order);
              }  catch(Exception e) {
                 // log exception and roll back completely
              } 
              if("handleDupl".equalsTo(dbAction) {
                    serviceBean.handleDuplOrder(order);
              }
          }
          myService.updateCustomer(customer);
       }
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>@Autowired
  MyRepository repo;
  
  @Transactional(propagation = propagation.REQUIRES_NEW)
  @override
  public String inserOrder() {
    String dbAction = "";
    try {
         repo.insertOrderDao(order);
    } catch(all duplicate key exceptions like entityExist, persist, ConstraintVioaltion, DataIntegrity e) {
        dbAction = "handleDuplOrder";
    } catch(all other Exception except duplicate key e) {
          // roll back and throw exception to Facade layer
    } 
    return dbAction;
  }
  
  @Transactional(propagation = propagation.REQUIRES_NEW)
  @override
  public void handleDuplOrder(Order order) {
    try {
         repo.selectOrderDao(order);
         repo.updateOrder(order);
    } catch(Exception e) {
          // roll back and throw exception to Facade layer
    }
  }
  </code></pre>
  
  <p>Repository: </p>
  
  <pre><code>@PersistentContext(unitNmae = "MY_SHCEMA")
  EntityManager entityManager;
  
  @Override
  public void insertOrderDao(Order order) {
       entityManager.persist(order);
       entityManager.flush(); 
  }
  </code></pre>
  
  <p>Problem: </p>
  
  <p>When I send req with One customer who has single order, where the order is duplicate, I see PersistException is caught inside Service method and when it exists from Service method it also throws TransactionSystemException(nested exception is RollbackException: Transaction is marked as rollback only, could not commit JPA transaction) is thrown to Facade layer irrespective of the how I suppress the exception in inner transaction.</p>
  
  <p>Kindly advice If I can achieve Unit of work commit or rollback in this way.</p>
  
  <p>Expected: </p>
  
  <p>I want Spring's @Transactional to ignore Duplicate key exceptions by not rolling back and not affecting the next transaction.</p>