1 22 package org.jboss.ejb3.test.relationships; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 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 (SessionRemote.class) 40 public class SessionBean implements SessionRemote 41 { 42 @PersistenceContext 43 private EntityManager em; 44 45 public void createCategory() 46 { 47 Category category = new Category(); 48 category.setId(1); 49 em.persist(category); 50 } 51 52 public void createItem() 53 { 54 System.out.println("++++++++++++++++++++++"); 55 Category category = em.find(Category.class, 1L); 56 Item item = new Item(); 57 item.setId(1); 58 item.setName("hello"); 59 category.getItems().add(item); 60 } 61 62 public int getNumItems() 63 { 64 Category category = em.find(Category.class, 1L); 65 for (Item i : category.getItems()) 66 { 67 System.out.println(i.getName()); 68 } 69 return category.getItems().size(); 70 } 71 72 public long createCustomer() 73 { 74 Customer cust = new Customer(); 75 CustomerRecord record = new CustomerRecord(); 76 cust.setCustomerRecord(record); 77 em.persist(cust); 79 return cust.getId(); 80 } 81 82 public void testInverse(long id) 83 { 84 CustomerRecord record = em.find(CustomerRecord.class, id); 85 record.setCustomer(null); 86 } 87 88 89 public Customer getCustomer(long id) 90 { 91 return em.find(Customer.class, id); 92 } 93 94 public long creatOrder() 95 { 96 Order order = new Order(); 97 em.persist(order); 98 LineItem beer = new LineItem(); 99 beer.setProduct("beer"); 100 LineItem wine = new LineItem(); 101 wine.setProduct("wine"); 102 103 beer.setOrder(order); 104 em.persist(beer); 105 wine.setOrder(order); 106 em.persist(wine); 107 return order.getId(); 108 } 109 110 public Order getOrder(long id) 111 { 112 return em.find(Order.class, id); 113 } 114 115 public void deleteOne(long itemId) 116 { 117 LineItem item = em.find(LineItem.class, itemId); 118 item.getOrder().getItems().remove(item); 119 em.remove(item); 120 } 121 122 public void deleteFromCollection(long orderId) 123 { 124 Order order = em.find(Order.class, orderId); 125 Collection <LineItem> items = order.getItems(); 126 Iterator <LineItem> it = items.iterator(); 127 while (it.hasNext()) 128 { 129 LineItem item = it.next(); 130 it.remove(); 131 item.setOrder(null); 132 break; 133 } 134 System.out.println("****** " + order.getItems().size()); 135 } 136 137 public void merge(Order order) 138 { 139 em.merge(order); 140 } 141 } 142 | Popular Tags |