1 22 package org.jboss.ejb3.test.entity; 23 24 import org.hibernate.Session; 25 import org.jboss.annotation.JndiInject; 26 27 import java.util.ArrayList ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.Set ; 31 import javax.ejb.Remote ; 32 import javax.ejb.Stateless ; 33 import javax.ejb.TransactionAttribute ; 34 import javax.ejb.TransactionAttributeType ; 35 import javax.persistence.EntityManager; 36 import javax.persistence.PersistenceContext; 37 import javax.persistence.Query; 38 import javax.transaction.TransactionManager ; 39 import javax.transaction.Transaction ; 40 import javax.transaction.SystemException ; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 45 46 52 @Stateless 53 @Remote (EntityTest.class) 54 public class EntityTestBean implements EntityTest 55 { 56 private @PersistenceContext EntityManager manager; 57 private @PersistenceContext Session session; 58 private @JndiInject(jndiName="java:/TransactionManager") TransactionManager tm; 59 private static Log log = LogFactory.getLog( "org.hibernate.ejb" ); 60 61 62 @TransactionAttribute (TransactionAttributeType.NOT_SUPPORTED) 63 public void testOutsideTransaction() 64 { 65 Transaction tx = null; 66 try 67 { 68 tx = tm.getTransaction(); 69 } 70 catch (SystemException e) 71 { 72 throw new RuntimeException (e); 73 } 74 if (tx != null) throw new RuntimeException ("THERE IS A TRANSACTION!!!"); 75 Query q = manager.createQuery("SELECT c FROM Customer c"); 76 List l = q.getResultList(); 77 if (l.size() == 0) throw new RuntimeException ("failed"); 78 org.hibernate.Query q2 = session.createQuery("FROM Customer c"); 79 l = q2.list(); 80 if (l.size() == 0) throw new RuntimeException ("failed"); 81 82 } 83 84 public Customer oneToManyCreate() throws Exception 85 { 86 Ticket t = new Ticket(); 87 t.setNumber("33A"); 89 Customer c = new Customer(); 90 Set <Ticket> tickets = new HashSet <Ticket>(); 92 tickets.add(t); 93 t.setCustomer(c); 94 c.setTickets(tickets); 95 Address address = new Address(); 96 address.setStreet("Clarendon Street"); 97 address.setCity("Boston"); 98 address.setState("MA"); 99 address.setZip("02116"); 100 c.setAddress(address); 101 manager.persist(c); 102 return c; 103 } 104 105 public Customer findCustomerById(Long id) throws Exception 106 { 107 return manager.find(Customer.class, id); 108 } 109 110 public Flight manyToOneCreate() throws Exception 111 { 112 Flight firstOne = new Flight(); 113 firstOne.setId(new Long (1)); 114 firstOne.setName("AF0101"); 115 Company frenchOne = new Company(); 116 frenchOne.setName("Air France"); 117 firstOne.setCompany(frenchOne); 118 manager.persist(firstOne); 119 return firstOne; 120 } 121 122 public void manyToManyCreate() throws Exception 123 { 124 125 Flight firstOne = findFlightById(new Long (1)); 126 Flight second = new Flight(); 127 second.setId(new Long (2)); 128 second.setName("US1"); 129 Company us = new Company(); 130 us.setName("USAir"); 131 second.setCompany(us); 132 133 Set <Customer> customers1 = new HashSet <Customer>(); 134 Set <Customer> customers2 = new HashSet <Customer>(); 135 136 137 Customer bill = new Customer(); 138 bill.setName("Bill"); 139 customers1.add(bill); 140 141 Customer monica = new Customer(); 142 monica.setName("Monica"); 143 customers1.add(monica); 144 145 Customer molly = new Customer(); 146 molly.setName("Molly"); 147 customers2.add(molly); 148 149 firstOne.setCustomers(customers1); 150 second.setCustomers(customers2); 151 152 manager.persist(second); 153 } 154 155 156 public Flight findFlightById(Long id) throws Exception 157 { 158 return manager.find(Flight.class, id); 159 } 160 161 public Company findCompanyById(Integer id) throws Exception 162 { 163 return manager.find(Company.class, id); 164 } 165 166 public FieldCustomer fieldOneToManyCreate() throws Exception 167 { 168 FieldTicket t = new FieldTicket(); 169 t.setNumber("33A"); 171 FieldCustomer c = new FieldCustomer(); 172 Set <FieldTicket> tickets = new HashSet <FieldTicket>(); 174 tickets.add(t); 175 t.setCustomer(c); 176 c.setTickets(tickets); 177 FieldAddress address = new FieldAddress(); 178 address.setStreet("Clarendon Street"); 179 address.setCity("Boston"); 180 address.setState("MA"); 181 address.setZip("02116"); 182 c.setAddress(address); 183 manager.persist(c); 184 return c; 185 } 186 187 public FieldCustomer fieldFindCustomerById(Long id) throws Exception 188 { 189 return manager.find(FieldCustomer.class, id); 190 } 191 192 public FieldFlight fieldManyToOneCreate() throws Exception 193 { 194 FieldFlight firstOne = new FieldFlight(); 195 firstOne.setId(new Long (1)); 196 firstOne.setName("AF0101"); 197 FieldCompany frenchOne = new FieldCompany(); 198 frenchOne.setName("Air France"); 199 firstOne.setCompany(frenchOne); 200 manager.persist(firstOne); 201 return firstOne; 202 } 203 204 public void fieldManyToManyCreate() throws Exception 205 { 206 207 FieldFlight firstOne = fieldFindFlightById(new Long (1)); 208 FieldFlight second = new FieldFlight(); 209 second.setId(new Long (2)); 210 second.setName("US1"); 211 FieldCompany us = new FieldCompany(); 212 us.setName("USAir"); 213 second.setCompany(us); 214 215 Set <FieldCustomer> customers1 = new HashSet <FieldCustomer>(); 216 Set <FieldCustomer> customers2 = new HashSet <FieldCustomer>(); 217 218 219 FieldCustomer bill = new FieldCustomer(); 220 bill.setName("Bill"); 221 customers1.add(bill); 222 223 FieldCustomer monica = new FieldCustomer(); 224 monica.setName("Monica"); 225 customers1.add(monica); 226 227 FieldCustomer molly = new FieldCustomer(); 228 molly.setName("Molly"); 229 customers2.add(molly); 230 231 firstOne.setCustomers(customers1); 232 second.setCustomers(customers2); 233 234 manager.persist(second); 235 } 236 237 238 public FieldFlight fieldFindFlightById(Long id) throws Exception 239 { 240 return manager.find(FieldFlight.class, id); 241 } 242 243 public FieldCompany fieldFindCompanyById(Integer id) throws Exception 244 { 245 return manager.find(FieldCompany.class, id); 246 } 247 248 public void testNamedQueries() throws Exception 249 { 250 System.out.println("testNamedQueries()"); 251 ArrayList ids = new ArrayList (); 252 Airport ap1 = new Airport("OSL", "Oslo"); 253 manager.persist(ap1); 254 255 Airport ap2 = new Airport("LHR", "London"); 256 manager.persist(ap2); 257 258 Airport ap3 = new Airport("LAX", "Los Angeles"); 259 manager.persist(ap3); 260 261 List list = manager.createNamedQuery("allAirports").getResultList(); 262 if (list.size() != 3) throw new RuntimeException ("Wrong number returned for allAirports query " + list.size()); 263 264 Airport ap = (Airport)manager.createNamedQuery("airportById").setParameter("id", ap2.getId()).getSingleResult(); 265 if (ap == null) throw new RuntimeException ("No object returned by airportById query"); 266 267 FieldAirport fap1 = new FieldAirport("LGW", "London"); 268 manager.persist(fap1); 269 270 FieldAirport fap2 = new FieldAirport("ORL", "Paris"); 271 manager.persist(fap2); 272 273 FieldAirport fap = (FieldAirport)manager.createNamedQuery("airportByCode").setParameter("code", "LGW").getSingleResult(); 274 if (fap == null) throw new RuntimeException ("No object returned by airportById query"); 275 } 276 277 public Customer createCustomer(String name) { 278 Customer c = new Customer(); 279 c.setName(name); 280 manager.persist(c); 281 return c; 282 } 283 284 public void changeCustomer(Long id, String name) { 285 Customer c = manager.find(Customer.class, id); 286 c.setName(name); 287 } 288 289 public Customer loadCustomer(Long id) { 290 Customer c = manager.find(Customer.class, id); 291 return c; 292 } 293 } 294 | Popular Tags |