1 22 package org.jboss.ejb3.test.manytomany; 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 import javax.persistence.PersistenceContext; 31 32 38 @Stateless 39 @Remote (EntityTest.class) 40 public class EntityTestBean implements EntityTest 41 { 42 private @PersistenceContext EntityManager manager; 43 44 public Customer oneToManyCreate() throws Exception 45 { 46 Ticket t = new Ticket(); 47 t.setNumber("33A"); 49 Customer c = new Customer(); 50 Set <Ticket> tickets = new HashSet <Ticket>(); 52 tickets.add(t); 53 t.setCustomer(c); 54 c.setTickets(tickets); 55 Address address = new Address(); 56 address.setStreet("Clarendon Street"); 57 address.setCity("Boston"); 58 address.setState("MA"); 59 address.setZip("02116"); 60 c.setAddress(address); 61 manager.persist(c); 62 return c; 63 } 64 65 public Customer findCustomerById(Long id) throws Exception 66 { 67 return manager.find(Customer.class, id); 68 } 69 70 public Flight manyToOneCreate() throws Exception 71 { 72 Flight firstOne = new Flight(); 73 firstOne.setId(new Long (1)); 74 firstOne.setName("AF0101"); 75 Company frenchOne = new Company(); 76 frenchOne.setName("Air France"); 77 firstOne.setCompany(frenchOne); 78 manager.persist(firstOne); 79 return firstOne; 80 } 81 82 public void manyToManyCreate() throws Exception 83 { 84 85 Flight firstOne = findFlightById(new Long (1)); 86 Flight second = new Flight(); 87 second.setId(new Long (2)); 88 second.setName("US1"); 89 Company us = new Company(); 90 us.setName("USAir"); 91 second.setCompany(us); 92 93 Set <Customer> customers1 = new HashSet <Customer>(); 94 Set <Customer> customers2 = new HashSet <Customer>(); 95 96 97 Customer bill = new Customer(); 98 bill.setName("Bill"); 99 customers1.add(bill); 100 101 Customer monica = new Customer(); 102 monica.setName("Monica"); 103 customers1.add(monica); 104 105 Customer molly = new Customer(); 106 molly.setName("Molly"); 107 customers2.add(molly); 108 109 firstOne.setCustomers(customers1); 110 second.setCustomers(customers2); 111 112 manager.persist(second); 113 } 114 115 116 public Flight findFlightById(Long id) throws Exception 117 { 118 return manager.find(Flight.class, id); 119 } 120 121 public Company findCompanyById(Integer id) throws Exception 122 { 123 return manager.find(Company.class, id); 124 } 125 126 } 127 | Popular Tags |