package session;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import domain.Customer;
import domain.Order;


public class XaJpaDemoBean {

	@PersistenceContext(name="customer") EntityManager customerEntityManager;
	@PersistenceContext(name="orders") EntityManager orderEntityManager;

	public void saveCustomerOrder(Customer c, Order o, Boolean succeeds) {
		try {
			EntityTransaction customerTransaction = customerEntityManager.getTransaction();
			customerTransaction.begin();

			// Update the customer entity in the database.
			customerEntityManager.merge(c);
			c.setNumberOfOrders(c.getNumberOfOrders() + 1);
			customerTransaction.commit();
			
			int cid = c.getId();
			System.out.println("Updated Customer with Id " + cid);
			
			EntityTransaction orderTransaction = orderEntityManager.getTransaction();
			orderTransaction.begin();

			// Insert the order entity in the database.
			orderEntityManager.persist(c);
			orderTransaction.commit();
			
			int oid = o.getId();
			System.out.println("Created order with Id " + oid);
			Query query = customerEntityManager.createQuery("select c from Customer c order by c.name");

			List<Customer> list = query.getResultList();
			System.out.println("There are " + list.size() + " persons:");
			for (Customer p : list) {
				System.out.println(p.getName());
			}
			System.out.println();
			
		} finally {	
			if (!succeeds) {
				throw new RuntimeException("Simulated failure!");
			}
		}
	}
}

@@ -26,20 +26,24 @@
 java
-.util.List;%0A
+x.ejb.Stateless;
 %0Aimp
@@ -171,40 +171,8 @@
 ext;
-%0Aimport javax.persistence.Query;
 %0A%0Aim
@@ -215,16 +215,26 @@
 Order;%0A%0A
+@Stateless
 %0Apublic 
@@ -818,16 +818,51 @@
 rintln(%22
+XaJpaDemoBean.saveCustomerOrder(): 
 Updated 
@@ -1165,343 +1165,72 @@
 ln(%22
-Created order with Id %22 + oid);%0A%09%09%09Query query = customerEntityManager.createQuery(%22select c from Customer c order by c.name%22);%0A%0A%09%09%09List%3CCustomer%3E list = query.getResultList();%0A%09%09%09System.out.println(%22There are %22 + list.size() + %22 persons:%22);%0A%09%09%09for (Customer p : list) %7B%0A%09%09%09%09System.out.println(p.getName());%0A%09%09%09%7D%0A%09%09%09System.out.println(
+XaJpaDemoBean.saveCustomerOrder(): Created order with Id %22 + oid
 );%0A%09
@@ -1299,16 +1299,51 @@
 eption(%22
+XaJpaDemoBean.saveCustomerOrder(): 
 Simulate

Annotate correctly (@Stateless). Correct several printouts.