1 22 package org.jboss.ejb3.test.composite; 23 24 import java.util.HashSet ; 25 import java.util.Set ; 26 import javax.ejb.Remote ; 27 import javax.ejb.Stateless ; 28 import javax.persistence.EntityManager; 29 import javax.persistence.PersistenceContext; 30 31 37 @Stateless 38 @Remote (EntityTest.class) 39 public class EntityTestBean implements EntityTest 40 { 41 private @PersistenceContext EntityManager manager; 42 private static long genid = 0; 43 44 public Customer oneToManyCreate(String name) throws Exception 45 { 46 Ticket t = new Ticket(); 47 t.setNumber("33A"); 48 Customer c = new Customer(); 49 CustomerPK pk = new CustomerPK(genid++, name); 50 c.setPk(pk); 51 Set <Ticket> tickets = new HashSet <Ticket>(); 52 tickets.add(t); 53 t.setCustomer(c); 54 c.setTickets(tickets); 55 manager.persist(c); 56 return c; 57 } 58 59 public Customer findCustomerByPk(CustomerPK pk) throws Exception 60 { 61 return manager.find(Customer.class, pk); 62 } 63 64 public Flight manyToOneCreate() throws Exception 65 { 66 Flight firstOne = new Flight(); 67 firstOne.setId(new Long (1)); 68 firstOne.setName("AF0101"); 69 manager.persist(firstOne); 70 return firstOne; 71 } 72 73 public void manyToManyCreate() throws Exception 74 { 75 76 Flight firstOne = findFlightById(new Long (1)); 77 Flight second = new Flight(); 78 second.setId(new Long (2)); 79 second.setName("US1"); 80 81 Set <Customer> customers1 = new HashSet <Customer>(); 82 Set <Customer> customers2 = new HashSet <Customer>(); 83 84 85 Customer will = new Customer(); 86 CustomerPK willPk = new CustomerPK(genid++, "Will"); 87 will.setPk(willPk); 88 customers1.add(will); 89 90 Customer monica = new Customer(); 91 CustomerPK moPK = new CustomerPK(genid++, "Monica"); 92 monica.setPk(moPK); 93 customers1.add(monica); 94 95 Customer molly = new Customer(); 96 CustomerPK mollyPK = new CustomerPK(genid++, "Molly"); 97 molly.setPk(mollyPK); 98 customers2.add(molly); 99 100 firstOne.setCustomers(customers1); 101 second.setCustomers(customers2); 102 103 manager.persist(second); 104 } 105 106 107 public Flight findFlightById(Long id) throws Exception 108 { 109 return manager.find(Flight.class, id); 110 } 111 112 113 public FieldCustomer fieldOneToManyCreate(String name) throws Exception 114 { 115 FieldTicket t = new FieldTicket(); 116 t.setNumber("33A"); 117 FieldCustomer c = new FieldCustomer(); 118 FieldCustomerPK pk = new FieldCustomerPK(genid++, name); 119 c.setPk(pk); 120 Set <FieldTicket> tickets = new HashSet <FieldTicket>(); 121 tickets.add(t); 122 t.setCustomer(c); 123 c.setTickets(tickets); 124 manager.persist(c); 125 return c; 126 } 127 128 public FieldCustomer fieldFindCustomerByPk(FieldCustomerPK pk) throws Exception 129 { 130 return manager.find(FieldCustomer.class, pk); 131 } 132 133 public FieldFlight fieldManyToOneCreate() throws Exception 134 { 135 FieldFlight firstOne = new FieldFlight(); 136 firstOne.setId(new Long (1)); 137 firstOne.setName("AF0101"); 138 manager.persist(firstOne); 139 return firstOne; 140 } 141 142 public void fieldManyToManyCreate() throws Exception 143 { 144 145 FieldFlight firstOne = fieldFindFlightById(new Long (1)); 146 FieldFlight second = new FieldFlight(); 147 second.setId(new Long (2)); 148 second.setName("US1"); 149 150 Set <FieldCustomer> customers1 = new HashSet <FieldCustomer>(); 151 Set <FieldCustomer> customers2 = new HashSet <FieldCustomer>(); 152 153 154 FieldCustomer will = new FieldCustomer(); 155 FieldCustomerPK willPk = new FieldCustomerPK(genid++, "Will"); 156 will.setPk(willPk); 157 customers1.add(will); 158 159 FieldCustomer monica = new FieldCustomer(); 160 FieldCustomerPK moPK = new FieldCustomerPK(genid++, "Monica"); 161 monica.setPk(moPK); 162 customers1.add(monica); 163 164 FieldCustomer molly = new FieldCustomer(); 165 FieldCustomerPK mollyPK = new FieldCustomerPK(genid++, "Molly"); 166 molly.setPk(mollyPK); 167 customers2.add(molly); 168 169 firstOne.setCustomers(customers1); 170 second.setCustomers(customers2); 171 172 manager.persist(second); 173 } 174 175 176 public FieldFlight fieldFindFlightById(Long id) throws Exception 177 { 178 return manager.find(FieldFlight.class, id); 179 } 180 181 182 } 183 | Popular Tags |