1 22 package org.jboss.ejb3.test.entitycallback; 23 24 import java.util.Iterator ; 25 import javax.ejb.Remote ; 26 import javax.ejb.Stateless ; 27 import javax.ejb.TransactionAttribute ; 28 import javax.ejb.TransactionAttributeType ; 29 import javax.persistence.EntityManager; 30 import javax.persistence.PersistenceContext; 31 import javax.persistence.PersistenceContext; 32 33 37 @Stateless 38 @TransactionAttribute (TransactionAttributeType.REQUIRED) 39 @Remote (EntityCallbackTest.class) 40 public class EntityCallbackTestBean implements EntityCallbackTest 41 { 42 private @PersistenceContext EntityManager entityManager; 43 44 public void createCustomer() 45 { 46 System.out.println("----createCustomer()"); 47 CallbackCounterBean.clear(); 48 Customer cust = new Customer("Kabir"); 49 entityManager.persist(cust); 50 System.out.println("----createCustomer() - END"); 51 } 52 53 public void addJourneysToCustomer() 54 { 55 System.out.println("----addJourneysToCustomer()"); 56 CallbackCounterBean.clear(); 57 Customer cust = entityManager.find(Customer.class, new Long (1)); 58 59 cust.addJourney(new TrainJourney("Oslo", "London", "T1")); 60 cust.addJourney(new TrainJourney("Oslo", "London", "T2")); 61 cust.addJourney(new BusJourney("Oslo", "London", "B1")); 62 cust.addJourney(new BusJourney("Oslo", "London", "B2")); 63 cust.addJourney(new BusJourney("Oslo", "London", "B3")); 64 System.out.println("----addJourneysToCustomer() - END"); 65 } 66 67 public void updateCustomer() 68 { 69 System.out.println("----updateCustomer()"); 70 CallbackCounterBean.clear(); 71 Customer cust = entityManager.find(Customer.class, new Long (1)); 72 73 cust.setName("Kabir Khan"); 74 System.out.println("----updateCustomer() -END"); 75 } 76 77 public void updateOneTrainJourney() 78 { 79 System.out.println("----updateOneTrainJourney()"); 80 CallbackCounterBean.clear(); 81 Customer cust = entityManager.find(Customer.class, new Long (1)); 82 83 for (Iterator it = cust.getJourneys().iterator(); it.hasNext();) 84 { 85 Object obj = it.next(); 86 if (obj instanceof TrainJourney) 87 { 88 TrainJourney trainJourney = (TrainJourney) obj; 89 String train = trainJourney.getTrain(); 90 trainJourney.setTrain(train + "-1"); 91 entityManager.flush(); 92 break; 93 } 94 } 95 System.out.println("----updateOneTrainJourney() - END"); 96 } 97 98 public void updateAllBusJourneys() 99 { 100 System.out.println("----updateAllBusJourneys()"); 101 CallbackCounterBean.clear(); 102 Customer cust = entityManager.find(Customer.class, new Long (1)); 103 104 for (Iterator it = cust.getJourneys().iterator(); it.hasNext();) 105 { 106 Object obj = it.next(); 107 if (obj instanceof BusJourney) 108 { 109 BusJourney busJourney = (BusJourney) obj; 110 String bus = busJourney.getBus(); 111 busJourney.setBus(bus + "-1"); 112 } 113 } 114 System.out.println("----updateAllBusJourneys() - EN"); 115 } 116 117 public void updateEverything() 118 { 119 System.out.println("----updateEverything()"); 120 CallbackCounterBean.clear(); 121 Customer cust = entityManager.find(Customer.class, new Long (1)); 122 cust.setName("Kabir"); 123 124 for (Iterator <Journey> it = cust.getJourneys().iterator(); it.hasNext();) 125 { 126 Journey journey = it.next(); 127 journey.setDest("Somewhere fun and sunny"); 128 } 129 System.out.println("----updateEverything() - END"); 130 } 131 132 public void createAndDeleteCustomer() 133 { 134 System.out.println("----createAndDeleteCustomer()"); 135 136 CallbackCounterBean.clear(); 137 Customer cust = new Customer("Djengis"); 138 entityManager.persist(cust); 139 140 entityManager.remove(cust); 141 System.out.println("----createAndDeleteCustomer()"); 142 } 143 144 145 public void deleteSomeJourneys() 146 { 147 System.out.println("----deleteSomeJourneys()"); 148 CallbackCounterBean.clear(); 149 Customer cust = entityManager.find(Customer.class, new Long (1)); 150 151 boolean deletedBus = false; 152 boolean deletedTrain = false; 153 154 for (Iterator <Journey> it = cust.getJourneys().iterator(); it.hasNext();) 155 { 156 Journey journey = (Journey) it.next(); 157 158 if (journey instanceof BusJourney && !deletedBus) 159 { 160 it.remove(); 161 entityManager.remove(journey); 162 deletedBus = true; 163 } 164 else if (journey instanceof TrainJourney && !deletedTrain) 165 { 166 it.remove(); 167 entityManager.remove(journey); 168 deletedTrain = true; 169 } 170 } 171 System.out.println("----deleteSomeJourneys() - END"); 172 } 173 174 public void deleteCustomerAndJourneys() 175 { 176 System.out.println("----deleteCustomerAndJourneys()"); 177 CallbackCounterBean.clear(); 178 Customer cust = entityManager.find(Customer.class, new Long (1)); 179 entityManager.remove(cust); 180 System.out.println("----deleteCustomerAndJourneys() - END"); 181 } 182 183 } 184 | Popular Tags |